2

I am using retrofit for http calls and gson for json parsing. Here is my json

 {
"-109.457154947154,26.4155454751248": [
{
  "data": {
    "date": "2017-13-13",
    "source_type": "Light",
    "table_value": 3,
    "condition": "Good"
  }
},
{
  "data": {
    "date": "2019-11-15",
    "source_type": "Light",
    "table_value": 4,
    "condition": "Very good"
  }
},
{
  "data": {
    "date": "2019-11-15",
    "source_type": "Heavy",
    "table_value": 3,
    "condition": "Good"
  }
}
],
 "-110.2324214532214,27.9288762948267": [
 {
  "data": {
    "date": "2017-13-13",
    "source_type": "Light",
    "table_value": 3,
    "condition": "Good"
  }
},
{
  "data": {
    "date": "2019-11-15",
    "source_type": "Light",
    "table_value": 4,
    "condition": "Very good"
  }
},
{
  "data": {
    "date": "2019-11-15",
    "source_type": "Heavy",
    "table_value": 3,
    "condition": "Good"
  }
}
]
}

This response does not have keys for top-level elements. I want to serialize this response.

Here is my data object.class

public class Data {

/*
*  "data": {
    "date": "2017-13-13",
    "source_type": "Light",
    "table_value": 3,
    "condition": "Good"
  }
*
* */

@SerializedName("date")
@Expose
private String date;

@SerializedName("source_type")
@Expose
private String source_type;

@SerializedName("table_value")
@Expose
private String table_value;

@SerializedName("condition")
@Expose
private String condition;

public Data(String date, String source_type, String table_value, String condition) {
    this.date = date;
    this.source_type = source_type;
    this.table_value = table_value;
    this.condition = condition;
}
   // Getters and Setters
 }

Since I am new to Android development I couldn't understand how to serialize other elements. I know I should write another Model class for parsing this JSON response. But I couldn't figure out the correct way of doing it.

Sunxt
  • 23
  • 3

2 Answers2

0

"-109.4571...", "-110.23..." are key, not value. If you want get key, you can see here.

If you want parse to object, you should repair json to:

{
    "string_key_1":"-109.457154947154,26.4155454751248",
    "string_key_2":[
        {
            "data": {
                "date": "2017-13-13",
                "source_type": "Light",
                "table_value": 3,
                "condition": "Good"
            }
        }
    ]
}
ngdathd
  • 23
  • 4
  • Thanks for the update but if you check the next json object, these -109.4571...", "-110.23..." are changed. It means those are dynamic. – Sunxt Oct 03 '19 at 09:15
0

Complete code to fetch this dynamic json response also from different key of jsonarray in your example:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<Data> arrayListData;
    arrayListData=new ArrayList<>();

   String jresponse="" +
           "{\n" +
           "   \"-109.457154947154,26.4155454751248\":[\n" +
           "      {\n" +
           "         \"data\":{\n" +
           "            \"date\":\"2017-13-13\",\n" +
           "            \"source_type\":\"Light\",\n" +
           "            \"table_value\":3,\n" +
           "            \"condition\":\"Good\"\n" +
           "         }\n" +
           "      },\n" +
           "      {\n" +
           "         \"data\":{\n" +
           "            \"date\":\"2019-11-15\",\n" +
           "            \"source_type\":\"Light\",\n" +
           "            \"table_value\":4,\n" +
           "            \"condition\":\"Very good\"\n" +
           "         }\n" +
           "      },\n" +
           "      {\n" +
           "         \"data\":{\n" +
           "            \"date\":\"2019-11-15\",\n" +
           "            \"source_type\":\"Heavy\",\n" +
           "            \"table_value\":3,\n" +
           "            \"condition\":\"Good\"\n" +
           "         }\n" +
           "      }\n" +
           "   ],\n" +
           "   \"-110.2324214532214,27.9288762948267\":[\n" +
           "      {\n" +
           "         \"data\":{\n" +
           "            \"date\":\"2017-13-13\",\n" +
           "            \"source_type\":\"Light\",\n" +
           "            \"table_value\":3,\n" +
           "            \"condition\":\"Good\"\n" +
           "         }\n" +
           "      },\n" +
           "      {\n" +
           "         \"data\":{\n" +
           "            \"date\":\"2019-11-15\",\n" +
           "            \"source_type\":\"Light\",\n" +
           "            \"table_value\":4,\n" +
           "            \"condition\":\"Very good\"\n" +
           "         }\n" +
           "      },\n" +
           "      {\n" +
           "         \"data\":{\n" +
           "            \"date\":\"2019-11-15\",\n" +
           "            \"source_type\":\"Heavy\",\n" +
           "            \"table_value\":3,\n" +
           "            \"condition\":\"Good\"\n" +
           "         }\n" +
           "      }\n" +
           "   ]\n" +
           "}";


   try {
       JSONObject jsonObject=new JSONObject(jresponse);

       JSONArray jsonArrayNames=jsonObject.names();


       for (int n=0;n<jsonArrayNames.length();n++)
       {

           //JSONArray jsonArray=jsonObject.getJSONArray("-109.457154947154,26.4155454751248");
           Log.d("jsonarray = ",jsonArrayNames.getString(n)+"");
           String sjsonarrayname=jsonArrayNames.getString(n)+"";
           JSONArray jsonArray=jsonObject.getJSONArray(sjsonarrayname);

           if(jsonArray.length()>0)
           {
               for(int i=0;i<jsonArray.length();i++)
               {
                   JSONObject xobj=jsonArray.getJSONObject(i);
                   JSONObject data_object=xobj.getJSONObject("data");

                   String date=data_object.getString("date");
                   String source_type=data_object.getString("source_type");
                   String table_value=data_object.getString("table_value");
                   String condition=data_object.getString("condition");

                   Toast.makeText(getApplicationContext(),date+"\n"+source_type+"\n"+table_value+"\n"+condition,Toast.LENGTH_LONG).show();

                   Log.d("date=",date+"");
                   Log.d("source_type=",source_type+"");
                   Log.d("table_value=",table_value+"");
                   Log.d("condition=",condition+"");
                   Log.d("========","========");

                   arrayListData.add(new Data(date,source_type,table_value,condition));
               }
           }

       }


   }
   catch (Exception e)
   {
                Log.d("error=",e+"");
   }


}