I know the difference between JSONArray and JSONObject.
I have doubt regarding deserialising them.
While deserialising an array of object type Cluster, I could do it by using fromJson object.
On the contrary while deserialising an object of type Topic, I had to use JSONParser.
What is the difference ? I can not make out when to use JSONParser.
FYI the Cluster class:
package com.example.android_json;
public class Cluster {
public String title;
public String stories;
public String src;
public Cluster()
{
}
public Cluster(String title,String stories,String src)
{
this.title = title;
this.stories = stories;
this.src = src;
}
}
The Topic class:
package com.example.news_android_mobile_application_cd;
import java.util.ArrayList;
import java.util.List;
import checkdeck.news.ui_services_java_api.rest.model.MiniCluster;
public class Topic {
public Topic()
{
}
public Topic(String TopicID,String TopicName,ArrayList<MiniCluster> miniCluster,ArrayList<String> clusterid)
{
topicName = TopicName;
topicID = TopicID;
clusterList = miniCluster;
clusterID = clusterid;
}
String topicID;
String topicName;
boolean isMandatory;
List<MiniCluster> clusterList = new ArrayList<MiniCluster>();
ArrayList<String> clusterID = new ArrayList<String>();
}
The deserialisation code is as follows -
For Cluster class :
Gson gson1 = new Gson();
Cluster[] clusters = gson1.fromJson(json, Cluster[].class);
For Topic class :
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonObject responseObj = parser.parse(json).getAsJsonObject();
String topicID = responseObj.getAsJsonPrimitive("topicID").getAsString();
String topicName = responseObj.getAsJsonPrimitive("topicName").getAsString();
Boolean isMandatory = responseObj.getAsJsonPrimitive("isMandatory").getAsBoolean();
JsonArray cList = responseObj.getAsJsonArray("clusterList");
JsonArray cID = responseObj.getAsJsonArray("clusterID");
List<MiniCluster> clusterList = new ArrayList<MiniCluster>();
ArrayList<String> clusterID = new ArrayList<String>();
for(int i=0;i<cID.size();i++)
{
clusterList.add(gson.fromJson(cList.get(i), MiniCluster.class));
clusterID.add(cID.get(i).toString());
}
The JsonData to be deserialised
Thanks in advance.
TopicwithGson? Also, it would really help if you put a sample json in the question body.jsonI meant the actual String that you want to de-serialize