0

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"
            }
         ]
      }
   }
}
5
  • 4
    Please add more 'full stops' to your question. It seems more like reading a Virginia Woolf novel. Commented Dec 11, 2013 at 11:53
  • 1
    Please include the code you have tried. Commented Dec 11, 2013 at 11:55
  • Can you please include your code of parsing. Commented Dec 11, 2013 at 11:58
  • 1
    I second aquaraga's comment - please try and use punctuation, it will make your question easier to deciper. Commented 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. Commented Dec 11, 2013 at 12:29

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

JSONObject["project"] is not a JSONArr it throw this exception
I used projectsJsonObject.optJSONArray("project") instead of projectsJsonObject.getJSONArray("project") == null Anyways rest was fine thanks alot
@JunaidAkhtar I was expecting exception but at other place, but I'm glad it helped :)
0

Use Google Gson

JsonParser parser = new JsonParser();
JsonObject o = (JsonObject)parser.parse("{\"a\": \"A\"}");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.