0

如何解析这个json?我想从标题、字段位置和兄弟字段中获取数据。

{
"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");

...但它不起作用。

4

2 回答 2

1

在 JSON 字符串中,有两个符号可以引导您完成解析:

{ - 表示一个 JSONObject

[ - 表示一个 JSONArray

解析 json 字符串时,您应该迭代地遍历这些项目。要了解您的字符串中有多少 JsonObjects 和 JsonArrays,以及您应该从哪里开始解析,请使用像这个网站这样的 json-visualizer 工具。:
在此处输入图像描述

示例:如您所见,根对象是一个 JSONObject,它由一个带有三个 jsonOnject 的 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");
  }
于 2014-05-15T05:46:36.277 回答
-1

尝试这个

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();
        }
于 2014-05-15T05:41:49.763 回答