gpt4 book ai didi

java - Gson解析卡住

转载 作者:行者123 更新时间:2023-12-01 14:52:16 25 4
gpt4 key购买 nike

我有这个 .json 类型

{"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 的数组列表中,但现在我只有一个空循环。我收到的错误卡住在这一行:

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

最佳答案

  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);
    }

关于java - Gson解析卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14714738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com