0

私はこの.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();
4

1 に答える 1

1
  1. 有効なJSONファイルではありません。だから多分それがパーサーが動けなくなる理由です。詳細については、JSONlintで確認してください。カンマがありません。

  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);
    }
    
于 2013-02-05T18:58:27.343 に答える