2

My Json Array looks like this

 [
   {
      "geoRegion":"101",
      "companyName":"101",
      "department":"101",
      "industry":"101",
      "sector":"101",
      "formName":"101",
      "formType":true,
      "fields":[
         {
            "labelId":"102",
            "bizJust":"101"
         },
         {
            "labelId":"103",
            "bizJust":"101"
         },
         {
            "labelId":"104",
            "bizJust":"101"
         },
         {
            "labelId":"129",
            "bizJust":"102"
         },
         {
            "labelId":"128",
            "bizJust":"102"
         },
         {
            "labelId":"144",
            "bizJust":"102"
         },
         {
            "labelId":"143",
            "bizJust":"102"
         }
      ]
   }
]

My code is as following:

    public Map<String,String> createTransaction(String lang) throws Exception {

            Gson gson =new Gson();
            JsonObject jsonObj=gson.fromJson(lang, JsonObject.class);//Exception thrown at this line
}
1
  • The solution is to convert to a JsonArray when it's an array, and to a JsonObject when it's an object. Commented Jun 18, 2016 at 9:58

1 Answer 1

3

Try to get all the content into the JSON Array instead of the JSON Object.

Change your code from

JsonObject jsonObj=gson.fromJson(lang, JsonObject.class);

to:

JsonArray array = gson.fromJson(lang, JsonArray.class);
Sign up to request clarification or add additional context in comments.

2 Comments

But then how to fetch the values from this JsonArray
There is a few approaches how to parse JSON Array. You have to know what you need. You can, for example, define your classes get everything you need using gson (better). Or you can parse directly through the structure over loops to get some data, or something third...

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.