如何将json数组放在json对象中

时间:2017-06-08 11:41:03

标签: android arrays json

这是我正在使用的代码:

@Override
protected String doInBackground(String... arg0) {
    String yourJsonStringUrl = "GetCalender_Events";
    JsonParser jParser = new JsonParser();
    json = jParser.getJSONFromUrlArray(yourJsonStringUrl);
    return null;
}

@Override
protected void onPostExecute(String strFromDoInBg) {
    for (int i = 0; i < json.length(); i++) {
        try {
            JSONObject c = json.getJSONObject(i);
            ldatosAgenda.add(new DatosAgenda(c.getString("Event_Name"), c.getString("Event_Name"),
                    sdf5.format(sdf1.parse(c.getString("Column1"))), sdf6.format(sdf1.parse(c.getString("Column1"))),
                    c.getString("Description")));
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}

如何将我的JSON数组放入JSON对象?任何人都可以帮助我,这是我第一次使用JSON Array。

我的数组看起来像这样:

{
  "ContactList": [
    {
      "Column1": "22-05-2017",
      "Event_Name": "Garba Compition",
      "Description": "School organized garba compition"
    },
    {
      "Column1": "24-05-2017",
      "Event_Name": "Mahendi Compition",
      "Description": "Mahendi compition"
    }
  ]
}

2 个答案:

答案 0 :(得分:0)

JSONArray jsonarray = new JSONArray();
JSONObject jsonObject = new JSONObject();

try{
    jsonObject.put(jsonarray);
} catch(JSONException e){
    e.printStackTrace();
}

获得值

JSONArray songobj = json.getJSONArray("ContactList");
Log.d(TAG, "List Length" + songobj.length());
for (int i = 0; i < songobj.length(); i++) {
     JSONObject song = songobj.getJSONObject(i);
     String column=song.getString("Column1");
     String name=song.getString("Event_Name");
     String desc=song.getString("Description");
     ldatosAgenda.add(new DatosAgenda(column,name,desc));
  }
}

答案 1 :(得分:0)

#。似乎您收到JSONObject作为回复。所以你必须首先从服务器响应中解析JSONObject

JSONObject json = jParser.getJSONFromUrlObject(yourJsonStringUrl);

#。然后解析ContactList及其项目如下:

@Override
protected void onPostExecute(String strFromDoInBg) {

    JSONArray jsonArray = json.getJSONArray("ContactList");

    for (int i = 0; i < jsonArray.length(); i++) {
        try 
        {
            JSONObject c = jsonArray.getJSONObject(i);
            ldatosAgenda.add(new DatosAgenda(c.getString("Event_Name"),c.getString("Event_Name"), sdf5.format(sdf1.parse(c.getString("Column1"))), sdf6.format(sdf1.parse(c.getString("Column1"))),c.getString("Description")));
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}