Hello i call a service if it contains multiple objects it make list but when it contain only one object it return a single object not a list [] are missing , actually i want to convert them into java class using gson but in case of single exception it throw exception but when it contain list it work fine i actually need to convert my single gSON string to array ,please help me ..here is the string
{
"response":{
"projects":{
"project":{
"ixWorkflow":1,
"sEmail":"[email protected]",
"sPhone":"",
"ixProject":2,
"ixPersonOwner":2,
"fDeleted":false,
"sProject":"Project Default",
"fInbox":true,
"sPersonOwner":"junaid"
}
}
}
}
i want it to be like same as
{
"response":{
"projects":{
"project":[
{
"ixWorkflow":1,
"sEmail":"[email protected]",
"sPhone":"",
"ixProject":6,
"ixPersonOwner":2,
"fDeleted":false,
"sProject":"project 2",
"fInbox":false,
"sPersonOwner":"junaid"
},
{
"ixWorkflow":1,
"sEmail":"[email protected]",
"sPhone":"",
"ixProject":2,
"ixPersonOwner":2,
"fDeleted":false,
"sProject":"Project Default",
"fInbox":true,
"sPersonOwner":"junaid"
}
]
}
}
}
-
4Please add more 'full stops' to your question. It seems more like reading a Virginia Woolf novel.aquaraga– aquaraga2013年12月11日 11:53:17 +00:00Commented Dec 11, 2013 at 11:53
-
1Please include the code you have tried.user1907906– user19079062013年12月11日 11:55:54 +00:00Commented Dec 11, 2013 at 11:55
-
Can you please include your code of parsing.Chanchal Shelar– Chanchal Shelar2013年12月11日 11:58:49 +00:00Commented Dec 11, 2013 at 11:58
-
1I second aquaraga's comment - please try and use punctuation, it will make your question easier to deciper.ayahuasca– ayahuasca2013年12月11日 12:06:07 +00:00Commented Dec 11, 2013 at 12:06
-
You'd like to change "project" from a single element to an array? Well, either edit the JSON source to do that, or read it in with a simple parser, navigate through the Maps to "project", and replace the value of "project" with the appropriate List.Hot Licks– Hot Licks2013年12月11日 12:29:31 +00:00Commented Dec 11, 2013 at 12:29
2 Answers 2
With reference to https://stackoverflow.com/a/7284813/1105291
Please try below code before you pass json to Gson for object conversion, and please let me know if you get any error. Only posibility that I can see is exception at if.
JSONObject jsonObject = new JSONObject(responseString);
JSONObject projectsJsonObject = jsonObject.getJSONObject("response").getJSONObject("projects");
if(projectsJsonObject.getJSONArray("project") == null)
{
JSONArray jsonArray = new JSONArray();
jsonArray.put(projectsJsonObject.getJSONObject("project"));
projectsJsonObject.put("project", jsonArray);
}
//Pass jsonObject to Gson
3 Comments
Use Google Gson
JsonParser parser = new JsonParser();
JsonObject o = (JsonObject)parser.parse("{\"a\": \"A\"}");