簡體   English   中英

JSON解析-獲取值

[英]JSON Parsing- fetch value

我在解析此JSON時遇到問題。 我在JSONObject o中具有以下數據。 我如何獲取評論者評分的價值?

{
"total":1,"
 movies":
 [{
      "id":"770672122",
      "title":"Toy Story 3",
      "year":2010,
      "mpaa_rating":"G",
      "runtime":103,
      "critics_consensus":"Deftly blending comedy, adventure, and honest emotion, Toy   Story 3 is a rare second sequel that really works.",

      "release_dates":{"theater":"2010-06-18","dvd":"2010-11-02"},
      "ratings":
       {
                "critics_rating":"Certified Fresh",
                "critics_score":99,
                "audience_rating":"Upright", .......

謝謝

您可以執行以下操作:

    int criticsScore;
    try {
        criticsScore = myJsonObject.getJSONArray("movies").getJSONObject(0).getJSONObject("ratings").getInt("critics_score");
    } catch (JSONException e) {
        e.printStackTrace();
    }

編輯:這是假設您的JSONObject名為myJsonObject

看看我寫的有關在Android平台中解析JSON文件的博客文章,您可能會發現它很方便:

JSON解析

作為指示,您需要將movies對象解析為JSONArray ,然后獲取它的第一項並提取評級JSONObject ,然后從中提取critics_score int

data_array中[ '電影'] [0] [ '評分'] [ 'critics_score']

使用它來幫助讀取您的JSON- http://jsoneditoronline.org/

為了創建對象data_array,我使用了它,var data_array = $ .parseJSON('{“ total”:1,“ movies”:[{“ id”:“ 770672122”,“ title”:“玩具總動員3”,“年“:2010,” mpaa_rating“:” G“,”運行時間“:103,” critics_consensus“:”巧妙地融合喜劇,冒險和誠實的情感,《玩具總動員3》是罕見的第二部續作,確實有效。“,”發布日期“ :{“ theater”:“ 2010-06-18”,“ dvd”:“ 2010-11-02”},“ ratings”:{“ critics_rating”:“ Certified_fresh”,“ critics_score”:99,“ audience_rating” :“直立”}}]}');

Gson庫對於解析JSON確實很有幫助:

https://code.google.com/p/google-gson/

上面的Emil教程可以幫助您入門

其他人建議傑克遜,所以這里是一個將它們進行比較的stackoverflow線程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM