0

Hi I am using json parsing, Following is my json response. Can any one help me with parsing,

{"success":

"{"mydata":

[
["Ramesh","Architect","Surat","1","2011/04/25","$123,123"], 
["Suresh Ram","Accountant","Amdavad","2","2011/07/25","$121,121"],
["Naresh","Author","Up","3","2009/01/12","$76,000"],

........

My confusion is regarding parsing, response is very complex to parse.

chris
  • 699
  • 4
  • 12
  • 35

2 Answers2

0

I hope you can get success json object easily, so i am directly using jobSuccess here.

   ArrayList<DataModel> arrDataModel=new ArrayList<DataModel>();
        try {
            JSONArray jarMyData=jobSuccess.getJSONArray("mydata");
            for (int i = 0; i < jarMyData.length(); i++) {
                JSONArray jar = jarMyData.getJSONArray(i);

                    DataModel dataModel=new DataModel();
                    dataModel.name=jar.getString(0);
                    dataModel.occupation=jar.getString(1);
                    dataModel.place=jar.getString(2);
                    dataModel.id=jar.getString(3);
                    dataModel.date=jar.getString(4);
                    dataModel.price=jar.getString(5);
                    arrDataModel.add(dataModel);
            }
        }catch (JSONException e)
        {
            Log.d("JSONException",e.toString());
        }

You can create model class like this:

public class DataModel {
    String name, occupation, place, id, date, price;
}

Note: i am not sure about fields name, so change their name according to your need.

Update To get jobSuccess you need to :

String response="your json response in string format";
JSONObject jobSuccess=new JSONObject(response).getJSONObject("success");

Update 2

  // response is the json object you received from volley
            jobSuccess=response.getJSONObject("TABLE_DATA");

            ArrayList<DataModel> arrDataModel=new ArrayList<DataModel>();
            try {
// **Change below statement only**
                JSONArray jarMyData=new JSONObject(jobSuccess.getString("TABLE_DATA"));
                for (int i = 0; i < jarMyData.length(); i++) {
                    JSONArray jar = jarMyData.getJSONArray(i);

                        DataModel dataModel=new DataModel();
                        dataModel.name=jar.getString(0);
                        dataModel.occupation=jar.getString(1);
                        dataModel.place=jar.getString(2);
                        dataModel.id=jar.getString(3);
                        dataModel.date=jar.getString(4);
                        dataModel.price=jar.getString(5);
                        arrDataModel.add(dataModel);
                }
            }catch (JSONException e)
            {
                Log.d("JSONException",e.toString());
            }

Update Latest You need to change this:

JSONObject jobSuccess=response.getJSONObject("TABLE_DATA");

with this:

JSONArray jarMyData=new JSONObject(jobSuccess.getString("TABLE_DATA"));
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
-1

here is how you can achieve it in Kotlin.

val obj = JSONObject(response)

        val arrayOfArrays = obj.getJSONArray("mydata")

        repeat(arrayOfArrays.length()){index->
            val innerArray = arrayOfArrays.getJSONArray(index)
            //either get the fields by their index
            val name = innerArray.getString(0) //Ramesh
            val field = innerArray.getString(1)//Architect
            val third = innerArray.getString(2)//Surat
            val id = innerArray.getString(3)//1
            val date = innerArray.getString(4)//2011/04/25
            val price = innerArray.getString(5)//$123,123
            //or loop through the inner array
            repeat(innerArray.length()){
                //your inner array fields
            }
        }

suggestion:

  1. ask your backend developer to provide you with a list of objects instead of array of strings.

  2. use a JSON parsing library like: moshi, gson, jakson

seyed Jafari
  • 1,235
  • 10
  • 20