简体   繁体   English

JSONArray 用 gson 解析

[英]JSONArray parse with gson

It is working when I am trying to retrieve jsonobject through gson but when I use gson for retrieving jsonArray it's not working.当我尝试通过 gson 检索 jsonobject 时它正在工作,但是当我使用 gson 检索 jsonArray 时它不起作用。 How can I retrieve data from the below jsonArray and display to a listview through GSON?如何从下面的 jsonArray 检索数据并通过 GSON 显示到列表视图?

Can anyone help me?谁能帮我?

My jsonArray我的 jsonArray

[
 {
    "node_title": "One Day camp",
    "nid": "202605",
    "Icon Image": {
        "fid": "146",
        "uid": "1",
        "filename": "10483937_741467599279220_8365483080196647009_n.jpg",
        "uri": "public://10483937_741467599279220_8365483080196647009_n.jpg",
        "filemime": "image/jpeg",
    }
 },
 { 
    "node_title": "University of Kerala",
    "nid": "202604",
    "Icon Image": {
        "fid": "145",
        "uid": "1",
        "filename": "careerguru.png",
        "uri": "public://careerguru.png",
        "filemime": "image/png",
    }
 }
]

I think the problem is the , after "filemime": "xxx"我认为问题是,"filemime": "xxx"

Edit: I think I misunderstood your question.编辑:我想我误解了你的问题。 venkatKA's answer is correct. venkatKA 的回答是正确的。

Typically you would build an object with the same characteristics as the json you expect to receive.通常,您会构建一个与您希望收到的 json 具有相同特征的对象。 For instance,例如,

public class MyJsonObject {

    private String node_title;
    private String nid;

    private static class IconImage {

        private String fid;
        private String uid;
        private String filename;
        private String uri;
        private String filemime;

    }

}

You would then loop through that JSONArray serializing them with Gson into an array of MyJsonObject's.然后,您将遍历该 JSONArray,使用 Gson 将它们序列化为 MyJsonObject 的数组。

(As for the inner class I know Gson can serialize the inner classes but i'm not 100% sure of the syntax to declare that inner class as your "Icon Image") (至于内部类,我知道 Gson 可以序列化内部类,但我不是 100% 确定将内部类声明为“图标图像”的语法)

My point is, Use Gson to serialize the Json response into objects easily/conveniently.我的观点是,使用 Gson 可以轻松/方便地将 Json 响应序列化为对象。 But if you're just trying to grab data from the response use the JSONObject classes.但是,如果您只是想从响应中获取数据,请使用 JSONObject 类。

assuming that the POJO class that you want to store the data is already created, ie: the single json object in your json array.假设您要存储数据的 POJO 类已经创建,即:json 数组中的单个 json 对象。

Lets assume that this class is Node.java and contains a single Node object.让我们假设这个类是 Node.java 并且包含一个 Node 对象。

You can get the list of the Node objects from the json using below code through Gson.您可以通过 Gson 使用以下代码从 json 中获取节点对象的列表。

Gson gson = new Gson();
Type listType = new TypeToken<List<Node>>(){}.getType();
List<Node> posts = (List<Node>) gson.fromJson(jsonOutput, listType);

Now, you can use the List and use it for showing the data in the list.现在,您可以使用 List 并用它来显示列表中的数据。

And yes, according to venkatKA 's answer the json is also wrong as it has an extra ","是的,根据venkatKA的回答,json 也是错误的,因为它有一个额外的“,”

Hope it helps...希望能帮助到你...

1.Remove , after the  "filemime": "image/jpeg", json error.

2.Parse Json to Gson

public class Array {
@SerializedName("node_title")
@Expose
private String nodeTitle;
@SerializedName("nid")
@Expose
private String nid;
@SerializedName("Icon Image")
@Expose
private com.example.IconImage IconImage;
public String getNodeTitle() {
return nodeTitle;
}

public String getNid() {
 return nid;
}
}


public class IconImage {

@SerializedName("fid")
@Expose
private String fid;
@SerializedName("uid")
@Expose
private String uid;
@SerializedName("filename")
@Expose
private String filename;
@SerializedName("uri")
@Expose
private String uri;
@SerializedName("filemime")
@Expose
private String filemime;

public String getFid() {
return fid;
}

public String getUid() {
return uid;
}

public String getFilename() {
return filename;
}

public String getUri() {
return uri;
}
public String getFilemime() {
return filemime;
}

I hope it helps you.

You have an excellent example here: Gson and deserializing an array of objects with arrays in it你在这里有一个很好的例子: Gson and deserializing an array of objects with arrays in it

So you will want to parse your Json string like this:所以你会想像这样解析你的 Json 字符串:

public class IconImage {
    int fid;
    int uid;
    String filename;
    String uri;
    String filemime;
}

public class Node {
    String node_title;
    int nid;
    @SerializedName("Icon Image") IconImage icon_image;
}

Gson gson = new Gson();
Node[] myNodes = gson.fromJson(yourJsonString, Node[].class);

You can have a class like this:你可以有一个这样的类:

class NodeList{
      List<Node> allnode;
}

and other class will be Node with your specific array item.其他类将是带有您特定数组项的节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM