0

Values cannot be parsed using retrofit for the following format.

I tried using 1. Arraylist 2. Array[Array[]]

But could not get the output.

{
ModuleEId: [
            [
            "Test_SFPCA",
            "SFPCA_0001",
             "SFPCA_0002"
            ],
            [
             "Android_SFPCA",
             "SFPCA_0003",
              ""
            ]
       ]
}
4
  • this is not a valid json format. Commented Aug 8, 2019 at 10:54
  • Yeah It is not. But need to pharse. Commented Aug 8, 2019 at 10:54
  • try with hashmap key value pair Commented Aug 8, 2019 at 10:57
  • Yes, Please check my demo model class code - @Daya Nithi Commented Aug 8, 2019 at 11:37

2 Answers 2

1

First of all correct your response format like this

{ 
"ModuleEId": [
    [
      "Test_SFPCA",
      "SFPCA_0001",
      "SFPCA_0002"
    ],
    [
      "Android_SFPCA",
      "SFPCA_0003",
      ""
    ]
  ]
}

You can parse using below code

try {

    JSONObject object = new JSONObject(response);
    JSONArray jsonArray = object.getJSONArray("ModuleEId");

    ArrayList<ArrayList<String>> mainArray = new ArrayList();
    for (int i = 0; i < jsonArray.length(); i++) {
        ArrayList<String> array = new ArrayList<>();
        JSONArray subJsonArray = jsonArray.getJSONArray(i);
        for (int j = 0; j < subJsonArray.length(); j++) {
            array.add(subJsonArray.getString(j));
        }
        mainArray.add(array);
    }

} catch (JSONException e) {
    e.printStackTrace();
}

OR

You can also create Model class

public class Demo{

    @SerializedName("ModuleEId")
    @Expose
    private ArrayList<ArrayList<String>> moduleEId;

    public ArrayList<ArrayList<String>> getModuleEId() {
        return moduleEId;
    }

    public void setModuleEId(ArrayList<ArrayList<String>> moduleEId) {
        this.moduleEId = moduleEId;
    }

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

2 Comments

Did you find that we have a array inside an array?
Any possibility using retrofit?
1

Here is the valid json for your invalid json:

{
    "ModuleEId": [
        [
            "Test_SFPCA",
            "SFPCA_0001",
            "SFPCA_0002"
        ],
        [
            "Android_SFPCA",
            "SFPCA_0003",
            ""
        ]
    ]
}

Now you can parse it using this pojo class:

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class CheckResponse {

   @SerializedName("ModuleEId")
   @Expose
   private List<List<String>> moduleEId = null;

   public List<List<String>> getModuleEId() {
      return moduleEId;
   }

   public void setModuleEId(List<List<String>> moduleEId) {
      this.moduleEId = moduleEId;
   }

}

The simple and easiest way to parse your json response is to use

http://www.jsonschema2pojo.org/

copy your response and paste it on jsonschema2pojo and select your class name. It will return you java pojo code. You can easily utilize that to parse it.

Important: But your json response should be valid.

Hope this will help you.

4 Comments

Please have a try for the above json and post the answer.
it is saying your json is invalid.
Yeah . The real problem is that.
Thanks man. Json response is from web development side. I know its invalid . THey could not fix it. Thanks for your Help @Nouman Ch

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.