无法解析2D json数组并显示其内容

正在从API获取2d数组,正在解析响应。但是我无法获得这些价值。


这就是得到回应的方式,


DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());

String api_uri = "http://192.168.0.106/api/something/";

HttpGet httpget = new HttpGet(api_uri);


httpget.setHeader("Content-type", "application/json");


InputStream inputStream = null;

String result = null;


try {

 HttpResponse response = httpclient.execute(httpget);

 HttpEntity entity = response.getEntity();


 inputStream = entity.getContent();

 // json is UTF-8 by default

 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);

 StringBuilder sb = new StringBuilder();


 String line = null;

 while ((line = reader.readLine()) != null) {

  sb.append(line + "\n");

 }

 result = sb.toString();

 Log.e("rAct", result);

} catch (Exception e) {

 // Oops

 Log.e("rActEr", e.toString());

} finally {

 try {

  if (inputStream != null) inputStream.close();

 } catch (Exception squish) {}

}

该result字符串如下,


[

    [

        {

            "id": 138,

            "name": "asdasd",

            "person_image": "http://something.in:9090/photos/14/1575.jpg",

            "person_description": "bla bla blaa",

            "bla_id": 1,

            "blabal_id": 2,

            "other_id": 7,

            "category": "asd"

        }

    ],

    [

        {

            "id": 257,

            "name": "asdasd",

            "person_image": "http://something.in/asd.jpg",

            "person_description": "asasdsad",

            "bla_id": 1,

            "blabal_id": 2,

            "other_id": 7,

            "category": "ASSSD"

        }

    ]

]

所以现在尝试读取数据,


try {

    jsonarray = new JSONArray(result);

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

        jsonobject = jsonarray.getJSONObject(i);

        Log.e("testt", jsonobject.toString());

        ...

        ...

    }

}catch....

我不能够从该得到什么jsonobject 如何获得的name,person_image,person_description从结果数据。我试图让他们喜欢jsonobject.optString("name"),但什么也没回来。


使用一维数组,我得到了响应,并且可以按照上面相同的方式读取数据。


我需要修改什么才能读取所需的数据?


长风秋雁
浏览 145回答 2
2回答

杨__羊羊

由于您具有数组数组,请尝试读取json数组而不是外部数组内部的对象:try {jsonarray = new JSONArray(result);for (int i = 0; i < jsonarray.length(); i++) {&nbsp; &nbsp; jsonarrayInner = jsonarray.getJSONArray(i);&nbsp; &nbsp; //you can do one more loop here&nbsp; &nbsp; for (int i = 0; i < jsonarrayInner.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; jsonobject = jsonarrayInner.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; Log.e("testt", jsonobject.toString());&nbsp; &nbsp; }&nbsp; &nbsp; ...&nbsp; &nbsp; ...}}catch....

GCT1015

我更喜欢在这种情况下将Gson与POJO一起使用,它将json数组包装到java.util.List of <YourPojo>List<MyPojo> myPojoList= new Gson().fromJson(jsonArray.toString(), new TypeToken<List<MyPojo>>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }.getType());并且您的pojo应该与您的json属性匹配所以你的课应该是这样的class MyPojo{&nbsp; &nbsp; private id;&nbsp; &nbsp; private String name ;&nbsp; &nbsp; private String person_image;&nbsp; &nbsp; private String person_description;&nbsp; &nbsp; private int bla_id;&nbsp; &nbsp; private int blabal_id;&nbsp; &nbsp; private int other_id;&nbsp; &nbsp; private String category;}这样,您将直接将json数组绑定到java对象
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java