2013-02-05 99 views
0

我有這個以.json類型GSON解析凍結

{"paging":{"page":1,"total":2015,"page_size":20,"pages":101}, 

"loans":[ 
    { 
    "id":519729, 
    "name":"Hefseba Group", 
    "description":{"languages":["en"]}, 
    "status":"fundraising", 
    "funded_amount":3125, 
    "basket_amount":0, 
    "image":{"id":1280581,"template_id":1}, 
    "activity":"Cafe", 
    "sector":"Food", 
    "use":"to buy more stock for her cafe, and to buy more fabrics and liquid soap.", 
    "location":{"country_code":"TZ", 
    "country":"Tanzania", 
    "geo":{"level":"country","pairs":"-6 35","type":"point"}}, 
    "partner_id":87, 
    "posted_date":"2013-02-01T09:14:17Z", 
    "planned_expiration_date":"2013-03-03T09:14:17Z", 
    "loan_amount":4025, 
    "borrower_count":16 
    }, 

    { 
    "id":520550,"name":"Waridi Pendo Group", 
    "description":{"languages":["en"]}, 
    "status":"fundraising", 
    "funded_amount":800, 
    "basket_amount":0, 
    "image":{"id":1282077,"template_id":1}, 
    "activity":"Music Discs & Tapes", 
    "sector":"Retail", 
    "use":"to buy music equipment and DVDs and CDs, as well as to start building his house. ", 
    "location":{"country_code":"TZ", 
    "country":"Tanzania", 
    "geo":{"level":"country","pairs":"-6 35","type":"point"}}, 
    "partner_id":87, 
    "posted_date":"2013-02-01T09:18:11Z", 
    "planned_expiration_date":"2013-03-03T09:18:11Z", 
    "loan_amount":6100, 
    "borrower_count":11 
    } 
] 

} 

然後,我有這樣的代碼試圖解析它。

try{ 
    JsonReader runThrough = getJsonReader(fileName); 
    System.out.println("File found"); 
    Gson myGson = new Gson(); 
    JsonParser jsonParser = new JsonParser(); 
    JsonArray infoArray = jsonParser.parse(runThrough).getAsJsonArray(); 
    System.out.println("Finished Parsing"); 
    for (JsonElement piece : infoArray){ 

    } 
} catch (Exception e){ 
    System.out.println(e); 
} 

我希望每個貸款最終被保存到kLoan的ArrayList,但我只是有一個空循環現在。錯誤我得到這一行凍結:

JsonArray infoArray = jsonParser.parse(runThrough).getAsJsonArray(); 
+0

有在'loans'列表的末尾逗號。 – santiagobasulto

+0

@santiagobasulto - 如果不是所有的JSON解析器都忽略了多餘的逗號,大多數情況下都是如此。 – rmlan

+0

我認爲在planned_expiration_date之後缺少的逗號可能是真正的問題。 – schlingel

回答

1
  1. 這不是一個有效的JSON文件。所以也許這就是解析器卡住的原因。有關詳細信息,請使用JSON lint。有一個逗號缺失。

  2. 整體對象不是數組。你需要獲得房產貸款,並調用此方法getAsJsonArray方法。像這樣:

    try{ 
        JsonReader runThrough = getJsonReader(fileName); 
        System.out.println("File found"); 
        Gson myGson = new Gson(); 
        JsonParser jsonParser = new JsonParser(); 
        JsonObject jsonObj = jsonParser.parse(runThrough).getAsJsonObject(); 
        JsonArray infoArray = jsonObj.getAsJsonArray("loans"); 
        System.out.println("Finished Parsing"); 
        for (JsonElement piece : infoArray){ 
    
        } 
    } catch (Exception e){ 
        System.out.println(e); 
    } 
    
+1

1實際上是不可能的,所以我認爲這是OP的錯誤/遺漏。 GSON的'JsonParser'會在未終結的字段上拋出'MalformedJsonException'。 2,但是,絕對正確。 – rmlan

+1

我不認爲逗號是問題。參考#2,我如何調用GetAsJsonArray來運行? – user2044299

+0

@ user2044299看到我編輯的答案。這很簡單。有關更多問題,請參閱以下文檔:http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonObject.html 順便說一句:您還可以定義模型類和讓GSON解析器爲您完成抽取工作。 – schlingel