简体   繁体   English

使用Jackson将JSON解析为对象

[英]Parsing JSON to Object using Jackson

I currently am using Jackson to parse my objects to and from JSON. 我目前正在使用Jackson来解析JSON中的对象。 Currently, translating the object to JSON works when I call toString() on the object. 当前,当我在对象上调用toString()时,可以将该对象转换为JSON。 But translating that JSON back into the object is becoming difficult. 但是将JSON转换回对象变得困难。 To test this working, I am running the following line: 为了测试这项工作,我正在运行以下行:

UserAppData test = UserAppData.appDataFromJSON(dummyUserAppData.toString());

This should return the same object that was started with (dummyUserAppData), however, I receive the following error when I run this: 这应该返回以(dummyUserAppData)开头的同一对象,但是,运行此命令时会收到以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{appuccino.droidpacks/com.appuccino.droidpacks.activities.MainActivity}: java.lang.RuntimeException: bad input {
"boughtAppIDs" : [ 1, 2, 3, 4, 5 ],
"boughtAppPacks" : [ {
"gold" : true,
"id" : 1
}, {
"gold" : false,
"id" : 2
}, {
"gold" : true,
"id" : 6
} ]
}
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.RuntimeException: bad input {
"boughtAppIDs" : [ 1, 2, 3, 4, 5 ],
"boughtAppPacks" : [ {
"gold" : true,
"id" : 1
}, {
"gold" : false,
"id" : 2
}, {
"gold" : true,
"id" : 6
} ]
}
        at com.appuccino.droidpacks.objects.UserAppData.appDataFromJSON(UserAppData.java:48)
        at com.appuccino.droidpacks.activities.MainActivity.onCreate(MainActivity.java:108)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
        ... 11 more

The classes that I'm using are as follows, where dummyAppUserData is of the class UserAppData: 我正在使用的类如下,其中dummyAppUserData属于UserAppData类:

public class UserAppData {

public int[] boughtAppIDs;
public UserPackData[] boughtAppPacks;

public UserAppData(int[] list, UserPackData[] packData){
    boughtAppIDs = list;
    boughtAppPacks = packData;
}

public static UserAppData appDataFromJSON(String json){
    if (json == null || json.isEmpty()) {
        return null;
    }
    ObjectMapper mapper = new ObjectMapper();
    ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
    UserAppData myObject;
    try {
        myObject = mapper.readValue(json, UserAppData.class);
        return myObject;
    }
    catch (  JsonParseException e) {
        e.printStackTrace();
        return null;
    }
    catch (  Exception e) {
        e.printStackTrace();
        throw new RuntimeException("bad input " + json);
    }
}

@Override
public String toString(){
    //ObjectWriter ow = new ObjectMapper().writer();//.withDefaultPrettyPrinter();
    ObjectMapper mapper = new ObjectMapper();
    try {
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
        //return ow.writeValueAsString(this);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        return null;
    }
}

} }

And this is the small class UserPackData that UserAppData has an instance of: 这是UserAppData具有以下实例的小类UserPackData:

public class UserPackData {
public int id;
public boolean gold;

public UserPackData(int i, boolean g){
    id = i;
    gold = g;
}

} }

What is it that I'm doing wrong? 我做错了什么事?

You haven't defined your constructors properly for Jackson to use them. 您尚未为Jackson正确定义构造函数以使用它们。 You need to identify each constructor as the constructor to use by annotating its parameters with @JsonProperty . 您需要通过使用@JsonProperty注释其构造函数来将每个构造函数标识为要使用的构造@JsonProperty

public UserAppData(@JsonProperty("boughtAppIDs") int[] boughtAppIDs, @JsonProperty("boughtAppPacks") UserPackData[] boughtAppPacks) {
    this.boughtAppIDs = boughtAppIDs;
    this.boughtAppPacks = boughtAppPacks;
}
...
public UserPackData(@JsonProperty("id") int id, @JsonProperty("gold") boolean gold) {
    this.id = id;
    this.gold = gold;
}

Alternatively, provide parameterless constructors and getters/setters (with appropriate @JsonProperty annotations). 或者,提供无参数的构造函数和getter / setter(带有适当的@JsonProperty批注)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM