Jackson JSON User Group

JSON in Java the Right Way -- Action, Jackson!

Deserialization with multi dimentional json arrays

Has anyone successfully binded json multi dimention arrays with a java object?  I've been racking my brain trying to figure this one out.  I can get the simple json example below to work but when trying to incorporate an array, and I have tried several different ways to make it work, I get either "Unrecognized field error" or the array to object type error, not sure of the verbiage.  My code is listed below this example.....If anyone can guide me as to how to bind an array from json to java, I would be greatly appreciative!!!!  Thank you in advance!

******** Sample Code that Works **********
 {
  "name" : { "first" : "Joe", "last" : "Sixpack" },
  "gender" : "MALE",
  "verified" : false,
  "userImage" : "Rm9vYmFyIQ=="
}
public class TestOrderForm {

    private Gender _gender;
    private Name _name;
    private boolean _isVerified;
    private byte[] _userImage;

    public enum Gender { MALE, FEMALE };

    public static class Name {
      private String _first, _last;

      public String getFirst() { return _first; }
      public void setFirst(String s) { _first = s; }

      public String getLast() { return _last; }
      public void setLast(String s) { _last = s; }
    }

    public Name getName() { return _name; }
    public void setName(Name n) { _name = n; }

    public boolean isVerified() { return _isVerified; }
    public void setVerified(boolean b) { _isVerified = b; }

    public Gender getGender() { return _gender; }
    public void setGender(Gender g) { _gender = g; }

    public byte[] getUserImage() { return _userImage; }
    public void setUserImage(byte[] b) { _userImage = b; }
}

*****************************  S t a r t   M y   C o d e *******************************************

{"orders_export":
    [{
        "info":{"orders_id":"250","date_purchased":"2010-05-18 16:10:50","orders_status":"Pending","last_modified":null}
    }]
}

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonContentClass;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.annotate.JsonPropertyOrder;
import org.codehaus.jackson.annotate.JsonTypeName;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.bim.ssp.pojo.AdBaseDirectory;

@SuppressWarnings("unused")
    public class OrderForm {
    // private static Logger logger = Logger.getLogger(OrderForm.class);

//    @JsonProperty("orders_export") public List<OrderInformation> _orders_export;
    @JsonProperty("orders_export") public static OrdersExport _orders_export;
    @JsonProperty("info") public OrderInformation _order_information;

    public static class OrdersExport {

        public List<OrderInformation> getOrder_Iformation() { return (List<OrderInformation>)     _orders_export; }
        public void setOrder_Information(List<OrderInformation> s) { _orders_export = (OrdersExport) s; }

    }

    public static class OrderInformation {
        @JsonProperty("orders_id") private String _orders_id;
        @JsonProperty("date_purchased") private String _date_purchased;
        @JsonProperty("orders_status") private String _orders_status;
        @JsonProperty("last_modified") private String _last_modified;

        public String getOrder_Id() { return _orders_id; }
        public void setOrder_Id(String s) { _orders_id = s; }

        public String getDate_Purchased() { return _date_purchased; }
        public void setDate_Purchased(String s) { _date_purchased = s; }

        public String getOrder_Status() { return _orders_status; }
        public void setOrder_Status(String s) { _orders_status = s; }


        public String getLastModified() { return _last_modified; }
        public void setLastModified(String s) { _last_modified = s; }

    }
    public OrderInformation getOrder_Information() { return _order_information; }
    public void setOrder_Information(OrderInformation s) { _order_information = s; }
}


Views: 2849

Reply to This

Replies to This Discussion

I think if I had an example of the online sample code below to include an array of "person" that would be very beneficial. What would the java object listed below look like? I'm trying to use the annotation @JsonProperty("person") public List names; to define an array of person. This gives me a "not recognized field error". My code follows:

{
"person":
[{
"name" : { "first" : "Joe", "last" : "Sixpack" }
}] ,
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="

}



public class TestOrderForm {

@JsonProperty("person") public List names;

private Gender _gender;
private Name _name;
private boolean _isVerified;
private byte[] _userImage;

public enum Gender { MALE, FEMALE };

public static class Name {
private String _first, _last;

public String getFirst() { return _first; }
public void setFirst(String s) { _first = s; }

public String getLast() { return _last; }
public void setLast(String s) { _last = s; }
}

public Name getName() { return _name; }
public void setName(Name n) { _name = n; }

public boolean isVerified() { return _isVerified; }
public void setVerified(boolean b) { _isVerified = b; }

public Gender getGender() { return _gender; }
public void setGender(Gender g) { _gender = g; }

public byte[] getUserImage() { return _userImage; }
public void setUserImage(byte[] b) { _userImage = b; }
}
DMac said:
I think if I had an example of the online sample code below to include an array of "person" that would be very beneficial. What would the java object listed below look like? I'm trying to use the annotation @JsonProperty("person") public List names; to define an array of person. This gives me a "not recognized field error". ... }

One immediate improvement: you have to declare type of List elements, for deserialization to work. So instead of:

public List names;

you need to use

public List names;

(or whatever type the elements are).

Serialization will work without type (since runtime type is used by default), but it can not be deserialized.
Or rather, elements of the List would be Maps with key/value pairs (since implied type is Listif type is omitted).
Has anyone a solution to this?  I have a similar data structure as orders_export and couldn't deserialize to Java object, particularly the "info" part.  Without the "info", there's no problem.
The simple rule is that structures should match: so one either has to remove unnecessary parts (that "info"), or add a wrapper class to match the structure.

Thanks for the quick reply. 

 

Sorry (didn't mean to send a private message).

 

The OrdersExport class above is not considered a "wrapper" class?  If not, then we'd have another class, say, OrderInformationShell that contains the OrderInformation and OrdersExport will contain a list of OrderInformationShell? Like this:

 

public class OrderInformation{

// fields, getters and setters

}

 

public class OrderInformationShell {

private OrderInformation o;

//getter and setter

}

 

public class OrdersExport{

private List<OrderInformationShell> s;

 

//getter and setter

}

 

RSS

© 2014   Created by Tatu Saloranta.   Powered by

Badges  |  Report an Issue  |  Terms of Service