繁体   English   中英

JSON解析Android-对象中的对象

[英]JSON parse Android - object in object

如何解析这个json? 我想从title,field_place和brothers字段获取数据。

{
"events": [
{
  "event": {
    "title": "BITUMIX Martyna Jastrz\u0119bska",
    "field_place": "Nowe Miejsce Al Jerozolimskie 51 lok.2",
    "field_zdjecie": "http:\/\/wybierzkulture.waw.pl\/sites\/default\/files\/styles\/post\/public\/martyna_jastrzebska_bitumix_2.jpg?itok=nd2O5U5z"
  }
},
{
  "event": {
    "title": "Wiecz\u00f3r Komedii Improwizowanej - D\u017cem Impro!",
    "field_place": "",
    "field_zdjecie": "http:\/\/wybierzkulture.waw.pl\/sites\/default\/files\/styles\/post\/public\/dzem_17_maja.jpg?itok=bfgDYxKq"
  }
}, 
...

我试过了:

JSONObject itemm = jArray.getJSONObject(i);
JSONObject oneObject = itemm.getJSONObject("event");
String title = oneObject.getString("title");
String field_place = oneObject.getString("field_place");

...但是它不起作用。

在JSON字符串中,有两个符号可指导您进行解析:

{-表示JSONObject

[-表示JSONArray

解析json字符串时,应迭代遍历此项目。 要了解您的字符串中有多少个JsonObjects和JsonArrays,以及应该从中开始解析,请使用类似于此网站的json-visualizer工具。
在此处输入图片说明

示例:如您所见,根对象是一个JSONObject,它由一个带有三个jsonOnjects的JSONArray组成。 要解析这样的结构,您可以使用:

JSONObject jsonobj = new JSONObject(jsonstring);

String result = jsonObject.getString("success");
String error_number = jsonObject.getString("error_number");    
String error_message = jsonObject.getString("error_message"); 

JSON Array jsonarray = jsonobj.getJSONArray();

String[] names = new String[jsonArray.length()];    
String[] formattedNames = new String[jsonArray.length()];  

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

    names [i] = jsonObject.getString("name");
    formattedNames [i] = jsonObject.getString("formattedName");
  }

尝试这个

JSONArray  element = jsonobj.getJSONArray("events");
              for(int i=0;i < element.length();i++){    
                    JSONObject e = element.getJSONObject(i);


                    Log.d("TESTTT22",e.getJSONObject("event").getString("title"));

              }


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM