3
[{"body":"{\"id\":\"act_105099966308460\",\"account_id\":105099966308460,\"name\":\"\",\"account_status\":2,\"currency\":\"USD\",\"timezone_id\":1,\"timezone_name\":\"America\\\/Los_Angeles\",\"timezone_offset_hours_utc\":-7,\"is_personal\":0,\"business_name\":\"\",\"business_street\":\"44916
Winding
Lane\",\"business_street2\":\"\",\"business_city\":\"Fremont\",\"business_state\":\"CA\",\"business_zip\":\"94539\",\"business_country_code\":\"US\",\"vat_status\":0,\"daily_spend_limit\":25000,\"users\":[{\"uid\":100004253709632,\"permissions\":[1,2,3,4,5,7],\"role\":1001}],\"notification_settings\":{\"100004253709632\":{\"1000\":{\"1\":1},\"1001\":{\"1\":1},\"1002\":{\"1\":1,\"2\":60},\"1003\":{\"1\":1,\"2\":60},\"1004\":{\"1\":1},\"1005\":{\"1\":1},\"1006\":{\"1\":1},\"1009\":{\"1\":1},\"1010\":{\"1\":1},\"1011\":{\"1\":1},\"2000\":{\"1\":1,\"2\":60},\"2001\":{\"1\":1,\"2\":60},\"2002\":{\"2\":60},\"2003\":{\"1\":1,\"2\":60},\"2004\":{\"1\":1,\"2\":60},\"2005\":{\"1\":1,\"2\":60},\"3000\":{\"1\":1,\"2\":60},\"3001\":{\"1\":1,\"2\":60},\"3002\":{\"2\":60},\"3003\":{\"2\":60},\"5000\":{\"1\":1},\"6000\":{\"1\":1},\"6001\":{\"1\":1},\"9000\":{\"1\":1,\"2\":60},\"8000\":{\"1\":1,\"2\":60}}},\"account_groups\":[{\"account_group_id\":344615332296926,\"name\":\"my
test
group\",\"status\":1},{\"account_group_id\":218621204934411,\"name\":\"inmobi
Ads
group\",\"status\":1},{\"account_group_id\":267052626739836,\"name\":\"Test
ad account
group1\",\"status\":1}],\"capabilities\":[2],\"balance\":0,\"moo_default_conversion_bid\":1000,\"amount_spent\":0}"}]

この文字列をオブジェクトとして解析したいのですが、失敗します。私が使用しているPojoとコード:

Type listType = new TypeToken<List<BatchAccounts>>(){}.getType();

Gson gson = new Gson();

List<BatchAccounts> results = gson.fromJson(response1, listType);

public class BatchAccounts
{
    private Account body;

    public Account getBody()
    {
        return body;
    }

}


public class Account
{

    private String id;

    private String account_id;

    public String getId()
    {
        return id;
    }

    public String getAccount_id()
    {
        return account_id;
    }

}

例外:

スレッド"main"com.google.gson.JsonParseExceptionの例外:予期するオブジェクトが見つかりました: "{\" id \ ":\" act_105099966308460 \ "、\" account_id \ ":105099966308460、\" name \ ":\" \ " 、\ "account_status \":2、\ "currency \":\ "USD \"、\ "timezone_id \":1、\ "timezone_name \":\ "America \\ / Los_Angeles \"、\ "timezone_offset_hours_utc \" :-7、\ "is_personal \":0、\ "business_name \":\ "\"、\ "business_street \":\ "44916 Winding Lane \"、\ "business_street2 \":\ "\"、\" business_city \ ":\" Fremont \ "、\" business_state \ ":\" CA \ "、\" business_zip \ ":\" 94539 \ "、\" business_country_code \ ":\" US \ "、\" vat_status \ ":0、\"daily_spend_limit \ ":25000、\" users \ ":[{\" uid \ "100004253709632、\ "permissions \":[1,2,3,4,5,7]、\ "role \":1001} ]、\ "notification_settings \":{\ "100004253709632 \":{\ "1000 \":{\ "1 \":1}、\ "1001 \":{\ "1 \":1}、\" 1002 \ ":{\" 1 \ ":1、\" 2 \ ":60}、\" 1003 \ ":{\" 1 \ ":1、\" 2 \ ":60}、\" 1004 \ ":{\" 1 \ ":1}、\" 1005 \ ":{\" 1 \ ":1}、\" 1006 \ ":{\" 1 \ ":1}、\" 1009 \ ": {\ "1 \":1}、\ "1010 \":{\ "1 \":1}、\ "1011 \":{\ "1 \":1}、\ "2000 \":{\ "1 \":1、\ "2 \":60}、\ "2001 \":{\ "1 \":1、\ "2 \":60}、\ "2002 \":{\ "2 \ ":60}、\" 2003 \ ":{\" 1 \ ":1、\" 2 \ ":60}、\ "2004 \":{\ "1 \":1、\ "2 \":60}、\ "2005 \":{\ "1 \":1、\ "2 \":60} 、\ "3000 \":{\ "1 \":1、\ "2 \":60}、\ "3001 \":{\ "1 \":1、\ "2 \":60}、\ "3002 \":{\ "2 \":60}、\ "3003 \":{\ "2 \":60}、\ "5000 \":{\ "1 \":1}、\ "6000 \ ":{\" 1 \ ":1}、\" 6001 \ ":{\" 1 \ ":1}、\" 9000 \ ":{\" 1 \ ":1、\" 2 \ ": 60}、\ "8000 \":{\ "1 \":1、\ "2 \":60}}}、\ "account_groups \":[{\ "account_group_id \":344615332296926、\ "name \" :\ "my test group \"、\ "status \":1}、{\ "account_group_id \":218621204934411、\ "name \":\ "inmobi Ads group \"、\ "status \":1}、 {\ "account_group_id \":267052626739836、\ "name \":\ "Test ad account group1 \"、\ "status \":1}]、\ "capabilities \":[2]、\ "balance \":0、\ "moo_default_conversion_bid \" :1000、\ "amount_spent \":0} "
    com.google.gson.JsonObjectDeserializationVisitor.visitFieldUsingCustomHandler(JsonObjectDeserializationVisitor.java:100)で
    com.google.gson.ReflectingFieldNavigator.visitFieldsReflectively(ReflectingFieldNavigator.java:63)で
    com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:120)で
4

1 に答える 1

0

他のJSONオブジェクト内にネストされたJSON文字列があるため、この構造を解析するには、次のような2つの解析手順が必要です。

Gson gson = new Gson();

List<BodyHolder> bodies = gson.fromJson(response1, 
   new TypeToken<List<BodyHolder>>() {}.getType());

List<BatchAccounts> result = new ArrayList<BatchAccounts>(bodies.size());
for (BodyHolder holder: bodies) {
    BatchAccounts a = new BatchAccounts();
    a.setBody(gson.fromJson(holder.getBody(), Account.class);
    result.add(a);
}

public class BodyHolder
{
    private String body;
    ...    
}

また、2番目のステップをカスタムとして実装することもできますJsonDeserializer

于 2012-09-10T10:25:15.353 に答える