如何从JSON"对象中获取价值"在Android中使用GSON

时间:2014-06-23 16:32:19

标签: android json gson

我的JSON响应看起来像

[{"order":-1,"artist":[{"name":"Hey"}]},...]

如何使用GSON从name对象获取artist


我试过用这样的东西

private String getArtistName(){
    ...
    Type type = new TypeToken<List<Order>>() {}.getType();
    List<Order> details = gson.fromJson(MyJSONresponse, type);
    for (Order order : details) {
        if (order.order == -1) {
            String artistName = // I don't know what to enter here
            return artistName;
        }
    }
    return null;
}

private class Order{
   int order;
   List<Artist> artist = new ArrayList<Artist>();
}

pricate class Artist{
   String name;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

class MyClass {
    int order;
    List<Artist> artist;
}

class Artist {
    String name;
}

MyClass myClass = new Gson().fromJson(myJsonString, MyClass[].class);
myClass.artist.get(0).name;