无法解析json'中的符号在android工作室

时间:2017-05-15 03:57:00

标签: android json

收到错误"无法从json'解析symbom在突出显示的(**)部分。

将json数组中的每个结果转换并解码为电影对象。 另外,下面是Json实现的方法中调用的方法。

public class BoxOfficeMovie implements Serializable {

private String title;
private int year;
private String synopsis;
private String posterlurl;
private int criticsScore;
private ArrayList<String> castlist;

public int getYear() {
    return year;
}

public String getSynopsis() {
    return synopsis;
}

public String getTitle() {
    return title;
}

public int getCriticsScore() {
    return criticsScore;
}

public String getPosterlurl() {
    return posterlurl;
}
public  String getCastList()
{
    return TextUtils.join(",",castlist);

}

public static BoxOfficeMovie fromJson(JSONObject jsonObject)
{
    BoxOfficeMovie b = new BoxOfficeMovie();
    try
    {
        //Deserialize json into object fields
        b.title= jsonObject.getString("title");
        b.year= jsonObject.getInt("Year");
        b.synopsis= jsonObject.getString("synopsis");
        b.posterlurl= jsonObject.getJSONObject("posters").getString("thumbnail");
        b.criticsScore= jsonObject.getJSONObject("ratings").getInt("critics_score");
        // Constrcuting a simple array of cast items
        b.castlist= new ArrayList<String>();
        JSONArray Cast = jsonObject.getJSONArray("Cast");
        for (int i=0;i<Cast.length();i++)
        {
            b.castlist.add(Cast.getJSONObject(i).getString("name"));
        }

    }
    catch (JSONException e)
    {
        e.printStackTrace();
        return null;

    }
    return b;
}      

public static ArrayList<BoxOfficeMovie> fromJson(JSONArray jsonArray)
{
    ArrayList<BoxOfficeMovie> movies= new ArrayList<BoxOfficeMovie>(jsonArray.length());
    // Process each result in json Array and convert and deccode into movie object
    JSONObject moviesJson;
    for (int i = 0; i<jsonArray.length(); i++)
        moviesJson = null;
    try
    {
        moviesJson= jsonArray.getJSONObject(i);
    }
    catch (Exception e )
    {
        e.printStackTrace();

    }

    **BoxOfficeMovie movie= new BoxOfficeMovie.fromJson(moviesJson);**
    if (movie!=null)
    {
        movies.add(movie);
    }

    return  movies;
}

2 个答案:

答案 0 :(得分:0)

嗨Vipul你的fromJson(JSONObject jsonObject)函数被声明为static。在这里

public static BoxOfficeMovie fromJson(JSONObject jsonObject)
{
    BoxOfficeMovie b = new BoxOfficeMovie();
    // your code
    return b;
} 

因此您需要从

更改方法调用
BoxOfficeMovie movie= new BoxOfficeMovie.fromJson(moviesJson);

BoxOfficeMovie movie= BoxOfficeMovie.fromJson(moviesJson);

静态字段和函数作为ClassName.field / function&amp; amp;创建对象以访问非静态字段。希望它可以帮到你。

让我们举一个简单的例子。假设我们有2个类A类和B类。这里是代码

Class A {

    public String name = "classA";   // public non-static

    public static String someFunction() {   //public static
         return "someValue";
    }
}

要访问另一个活动类B中的字段名称和someFunction,我们写

Class B extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // call static function of Class A
        String value = ClassA.someFunction();

        // access private non-static field as 
        ClassA classObject = new ClassA();
        String name = classObject.name;
    }
}

希望它有所帮助。

答案 1 :(得分:0)

您的方法public static BoxOfficeMovie fromJson(JSONObject jsonObject)是一种静态方法,您尝试创建instancecall此方法,并且它完全WRONG

无需创建类BoxOfficeMovie的实例来调用静态方法。您可以使用BoxOfficeMovie.fromJson(moviesJson)调用它。

更新您的方法fromJson(JSONArray jsonArray),如下所示:

public static ArrayList<BoxOfficeMovie> fromJson(JSONArray jsonArray)
{
    ArrayList<BoxOfficeMovie> movies= new ArrayList<BoxOfficeMovie>();

    try
    {
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject moviesJson = jsonArray.getJSONObject(i);

            BoxOfficeMovie movie = BoxOfficeMovie.fromJson(moviesJson);
            if (movie != null) {
                movies.add(movie);
            }
        }
    }
    catch (Exception e ) {
        e.printStackTrace();
    }

    return  movies;
}
相关问题