简体   繁体   中英

Parsing JSON to Object using Jackson

I currently am using Jackson to parse my objects to and from JSON. Currently, translating the object to JSON works when I call toString() on the object. But translating that JSON back into the object is becoming difficult. 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:

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:

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:

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. You need to identify each constructor as the constructor to use by annotating its parameters with @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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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