如何在没有密钥的情况下解析Json数组中的Json数组

时间:2018-11-24 04:35:21

标签: android arrays json jsonparser

嗨,我正在使用json解析,以下是我的json响应。谁能帮我解析,

{"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"],

........

我对解析感到困惑,解析响应非常复杂。

2 个答案:

答案 0 :(得分:0)

我希望您可以轻松地获得成功的json对象,所以我在这里直接使用jobSuccess

   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());
        }

您可以这样创建模型类:

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

注意:我不确定字段名称,因此请根据您的需要更改其名称。

更新 要获得jobSuccess,您需要:

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

更新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());
            }

更新最新 您需要更改此内容:

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

与此:

JSONArray jarMyData=new JSONObject(jobSuccess.getString("TABLE_DATA"));

答案 1 :(得分:-1)

这是在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
            }
        }

建议:

  1. 请您的后端开发人员为您提供对象列表,而不是字符串数组。

  2. 使用JSON解析库,例如:moshi,gson,jakson