3

I am facing json conversion exception. While i converting json to java object.

Here is my json

[  
   {  
      "PrefrenceId":"228f176d-d224-32d7-9bb5-6287a06a68e8",
      "UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
      "QuestionnaireId":"41f31b11-47f5-3e29-8c88-1a3615c978a7",
      "Suggestions":"",
      "Explanation":"",
      "IsActive":true,
      "IsDelete":false,
      "DateCreated":"2016-11-01 09:53:00.000",
      "DateUpdated":"2016-11-01 09:53:17.000"
   },
   {  
      "PrefrenceId":"52a74739-bdd3-33ac-a83f-72f60b1992b5",
      "UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
      "QuestionnaireId":"8cd5ac8e-89db-3d7b-bb2d-4e6735b245de",
      "Suggestions":"",
      "Explanation":"",
      "IsActive":true,
      "IsDelete":false,
      "DateCreated":"2016-11-01 09:48:53.000",
      "DateUpdated":"2016-11-01 09:53:15.000"
   },
   {  
      "PrefrenceId":"ae7fc877-b26a-34d3-a5f3-244c7e777e08",
      "UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
      "QuestionnaireId":"d3b98cde-111c-30d5-a4c9-412a76b656eb",
      "Suggestions":"Camping",
      "Explanation":"",
      "IsActive":true,
      "IsDelete":false,
      "DateCreated":"2016-11-01 09:53:02.000",
      "DateUpdated":"2016-11-01 09:53:19.000"
   },
   {  
      "PrefrenceId":"bcac0da7-31a6-345f-be82-ddff17c29b35",
      "UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
      "QuestionnaireId":"8fb1bda7-7ec8-3538-8aa8-ff84637764a4",
      "Suggestions":"",
      "Explanation":"",
      "IsActive":true,
      "IsDelete":false,
      "DateCreated":"2016-11-01 09:53:07.000",
      "DateUpdated":"2016-11-01 09:53:22.000"
   },
   {  
      "PrefrenceId":"ff46ce3c-70cb-3d25-8dbb-10e9c46d4c2d",
      "UserId":"327e6c64-bc90-3ae8-8f7d-72837581ca13",
      "QuestionnaireId":"3afffc17-30e4-311f-a0fc-8daa3bda6c98",
      "Suggestions":"",
      "Explanation":"",
      "IsActive":true,
      "IsDelete":false,
      "DateCreated":"2016-11-01 09:53:05.000",
      "DateUpdated":"2016-11-01 09:53:20.000"
   }
]

My POJO classes :-

public class SurvivorZAMQuestionList implements Serializable {
    public List<SurvivorZAMQuestionnaire> survivorZAMQuestionnaires;
}


public class SurvivorZAMQuestionnaire implements Serializable {
    public String Suggestions;

    public String PrefrenceId;

    public String IsActive;

    public String IsDelete;

    public String DateCreated;

    public String DateUpdated;

    public String UserId;

    public String QuestionnaireId;

    public String Explanation;
}

But when i am converting json response to json it is showing following error:- com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2

Can anyone tell me what i am missing in pojo class. Any kind of held should be appreciated.

2
  • from error, you need to change json response format or change your method for fetchig json response. Post your code for more detail solution Commented Nov 10, 2016 at 7:29
  • static <T> T getObject(String response, Class<T> clazz) { try { Object t = clazz.newInstance(); Field[] fields = clazz.getDeclaredFields(); Gson gson = new Gson(); return (T) gson.fromJson(response, clazz); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } @Divyesh i amd using above code for converting response to json Commented Nov 10, 2016 at 7:37

6 Answers 6

2

Your response is proper but your parsing is not proper. So first of all add GSON in your gradle file.

compile 'com.google.code.gson:gson:2.4'

Now use below your code for parsing your response

try {
        JSONArray array = new JSONArray("put your response here");
        Gson gson =  new Gson();
        for (int i = 0 ; i <array.length();i++)
        {
            SurvivorZAMQuestionnaire obj = new SurvivorZAMQuestionnaire();
            obj.add(gson.fromJson(array.getJSONObject(i).toString(),SurvivorZAMQuestionnaire.class));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

Add your obj in list and show it :)

Sign up to request clarification or add additional context in comments.

Comments

1

The Error clearly states that the Gson accepts JsonObject not JsonArray. In your case you can put the response JsonArray into a JsonObject with a key for that JsonArray and give that key as annotation in SurvivorZAMQuestionList. By this way you can easily sort this problem.

Hope this is Helpful :)

Comments

0

You can try to use GSON library -> compile 'com.google.code.gson:gson:2.8.0'

List<SurvivorZAMQuestionnaire> survivorZAMQuestionnaires;
...

Gson gson = new Gson();
Type listType = new TypeToken<List<SurvivorZAMQuestionnaire>>(){}.getType();
survivorZAMQuestionnaires = gson.fromJson(jsonString, listType);

Type is instance of java.lang.reflect.Type;

Comments

0

Parse Your Json this way,

List<SurvivorZAMQuestionnaire> survivorZAMQuestionnaires = new      Gson().fromJson(json, new TypeToken<List<SurvivorZAMQuestionnaire>>() {
    }.getType());

1 Comment

If you are parsing with "SurvivorZAMQuestionList" type then you need to add your complete json under a new JsonObject with key "survivorZAMQuestionnaires".
0

Do a gradle dependency of gson.

compile 'com.google.code.gson:gson:2.4'   

Update code like this;

 private void parseJSON(String jsonMessage) throws JSONException {
            if (jsonMessage.startsWith("[")) {
                Type type = new TypeToken<List<SurvivorZAMQuestionnaire>>() 
                }.getType();
                List<SurvivorZAMQuestionnaire> survivorZAMQuestionnaireList =new Gson().fromJson
                        (jsonMessage, type);
                Log.d("Print List",survivorZAMQuestionnaireList.toString());
            }

        }

1 Comment

Add some explanation with answer for how this answer help OP in fixing current issue
0

You need List of objects not only object, because your JSON contain list of objects. How to Parse JSON Array in Android with Gson

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.