将json字符串解析为json数组?

时间:2018-01-01 13:25:26

标签: java android json

我需要帮助将json字符串转换为json数组,如下面给出的

{"level":[{"id":"1","name":"first","time":"00:02:00"},{"id":"2","name":"second","time":"00:03:00"},{"id":"3","name":"math","time":"00:03:00"},
{"id":"4","name":"language ","time":"00:03:00"},{"id":"5","name":"sport","time":"00:04:00"}]}

在android studio中使用它

3 个答案:

答案 0 :(得分:3)

试试这个

try 
{    
    JSONObject resObject = new JSONObject("your json response");    
    JSONArray jsonArray = resObject.getJSONArray("level");        

    for (int i = 0; i < jsonArray.length(); i++) {  

       JSONObject jsonObject = jsonArray.getJSONObject(i);

       Log.i("id", "" + jsonObject.getString("id"));
       Log.i("name","" + jsonObject.getString("name"));
       Log.i("time", "" + jsonObject.getString("time"));

   }

} 
catch (JSONException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

<强>输出

enter image description here

答案 1 :(得分:1)

试试这个

    try {
        JSONObject jsonObject=new JSONObject(yourresponsestring);
        JSONArray jsonArray=new JSONArray(jsonObject.getJSONArray("level"));


        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject1=jsonArray.getJSONObject(i);
            String id=jsonObject1.getString("id");
            String name=jsonObject1.getString("name");
            String time=jsonObject1.getString("time");

        }

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

答案 2 :(得分:1)

您可以尝试以下代码将数据转换为JSON:

memoryStream.Write(arraySeg.Array, arraySeg.Offset, arraySeg.Count);