-3

It is working when I am trying to retrieve jsonobject through gson but when I use gson for retrieving jsonArray it's not working. How can I retrieve data from the below jsonArray and display to a listview through GSON?

Can anyone help me?

My 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",
    }
 }
]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

6 Answers6

1

I think the problem is the , after "filemime": "xxx"

venkatKA
  • 2,399
  • 1
  • 18
  • 22
0

Edit: I think I misunderstood your question. venkatKA's answer is correct.

Typically you would build an object with the same characteristics as the json you expect to receive. 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.

(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")

My point is, Use Gson to serialize the Json response into objects easily/conveniently. But if you're just trying to grab data from the response use the JSONObject classes.

Nick H
  • 8,897
  • 9
  • 41
  • 64
0

assuming that the POJO class that you want to store the data is already created, i.e: the single json object in your json array.

Lets assume that this class is Node.java and contains a single Node object.

You can get the list of the Node objects from the json using below code through Gson.

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.

And yes, according to venkatKA's answer the json is also wrong as it has an extra ","

Hope it helps...

Community
  • 1
  • 1
0
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.
Ramesh sambu
  • 3,577
  • 2
  • 24
  • 39
0

You have an excellent example here: Gson and deserializing an array of objects with arrays in it

So you will want to parse your Json string like this:

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);
Community
  • 1
  • 1
J-Cint
  • 661
  • 7
  • 8
-1

You can have a class like this:

class NodeList{
      List<Node> allnode;
}

and other class will be Node with your specific array item.

priyanka
  • 376
  • 2
  • 10
  • 1
    @down voter:Please comment while you downvote so that the ans can be improved or deleted. thanks:) – priyanka Jan 08 '16 at 12:08