传递JSON错误

时间:2016-02-25 11:58:16

标签: java android json

我正在尝试将JSON解析为一些字符串。一切似乎都没问题,但我得到同样的错误。我希望能找到帮助。

    {
  "response": {
    "status": "ok",
    "userTier": "developer",
    "total": 160689,
    "startIndex": 1,
    "pageSize": 1,
    "currentPage": 1,
    "pages": 160689,
    "orderBy": "newest",
    "results": [
      {
        "type": "article",
        "sectionId": "world",
        "webTitle": "Saudis lobby MEPs before arms embargo vote over Yemen",
        "webPublicationDate": "2016-02-25T06:01:25Z",
        "id": "world/2016/feb/25/saudis-lobby-meps-before-arms-embargo-vote-over-yemen",
        "fields": {
          "trailText": "Riyadh engaged in concerted effort to persuade European parliament not to pass amendment calling for EU sanctions because of bombing campaign",
          "thumbnail": "http://media.guim.co.uk/6646fa34096765c2e9456df41aa94d467cb22d7f/0_0_2835_1701/140.jpg"
        },
        "webUrl": "http://www.theguardian.com/world/2016/feb/25/saudis-lobby-meps-before-arms-embargo-vote-over-yemen",
        "sectionName": "World news"
      }
    ]
  }
}

这是我的解析代码:

    String title;
    String date;
    String text;
    Bitmap thumbnail;
    String webAddress;
    int dataSize;

    newsData = new ArrayList<>();
    JSONObject JSOnReceived = new JSONObject(JSON);
    JSONObject response = JSOnReceived.getJSONObject("response");
    String status = response.getString("status");
    if(status.equalsIgnoreCase("ok"))
    {
        dataSize = response.getInt("pageSize");
        JSONArray results = response.getJSONArray("results");
        int i = 0;
        while (i <= dataSize)
        {
            JSONObject data = results.getJSONObject(i);
            title = data.getString("webTitle");
            Log.v("ARRAY TITLE", title + "DONE");
            date = data.getString("webPublicationDate");
            Log.v("ARRAY DATE", title + "DONE");

            JSONObject fields = data.getJSONObject("fields");
            text = fields.getString("trailText");
            Log.v("ARRAY TRAILTEXT", title + "DONE");
            String imageUrl = fields.getString("thumbnail");
            thumbnail = getImage(imageUrl);
            webAddress = data.getString("webUrl");
            newsData.add(new NewsFacade(title, date, text, thumbnail, webAddress));
            i++;
            Log.v("ARRAY DATA", title + "DONE");

但是我从JSONException得到了这个错误,“找不到字段的值。我已将它更改为Field,FIELD。但同样的错误。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

你的while循环可能比实际长度多一次。将while循环条件更改为以下:

i < results.length(). 

它可能对你有帮助。

答案 1 :(得分:0)

我尝试解析result部分并且它有效:

String test = "{\"results\": [{ \"fields\": {\"trailText\": \"Riyadh engaged i because of bombing campaign \",\"thumbnail\": \"http://media.guim.co.uk/6646fa34096765c2e945f/0_0_2835_1701/140.jpg\"},\"webUrl\": \"http://www.theguardian.com/world/2016/feb/25/saudis-lobby-meps-before-arms-embargo-vote-over-yemen\"}    ]}";

        try {
            JSONObject testJson = new JSONObject(test);
            JSONArray arrayJSON = testJson.getJSONArray("results");
            JSONObject obj1 = arrayJSON.getJSONObject(0);
            Log.i("JSON", obj1.toString());
            JSONObject test1 = obj1.getJSONObject("fields");
            Log.i("FIELDS", test1.toString());
            String tmp = test1.getString("trailText");
            Log.i("FIELDS", tmp);
        } catch (JSONException e) {
            e.printStackTrace();
        }

答案 2 :(得分:0)

而不是:

final String baseUri = "http://content.guardianapis.com/search?";
        Uri uriBuilder = Uri.parse(baseUri)
                .buildUpon()
                .appendQueryParameter("section", "world")
                .appendQueryParameter("order-by", "newest")
                .appendQueryParameter("use-date", "published")
                .appendQueryParameter("show-fields", "trailText,thumbnail")
                .appendQueryParameter("page", "1")
                .appendQueryParameter("page-size", "1")
                .appendQueryParameter("api-key", "test")
                .build();

我这样做:     .appendQueryParameter(&#34; show-fields&#34;,&#34; trailText%2Cthumbnail&#34;)。

服务器忽略了那部分查询。