Deserialising arrays of json within objects within other objects - java

Probably a bit similar to this: deserializing json with arrays and also I'm following on from this: Jackson multiple objects and huge json files
The json I have is pretty big so simplified it goes something like this:
{ "foo"="bar", "x"="y", "z"=[{"stuff"="stuff"}, {"more"="stuff"}, ...], ... }
I have code that looks like this:
for (Iterator<Map> it = new ObjectMapper().readValues(new JsonFactory().createJsonParser(in), Map.class); it.hasNext();) {
doSomethingWith(it.next());
}
This works nicely at iterating over the objects in the file and I can get any value I like from the object I'm in. Works fine, but the array just comes back as an ArrayList of objects. So I have to do something like:
ArrayList z = (ArrayList) it.next().get("z");
for (Object o : z) {
// Run mapper on o.
// Do stuff.
}
I'm sure this would work, but it seems a bit messy to me. Is there a better way?

Oh, whoops. Looks like the first run of the ObjectMapper does everything for me.
So I can just change my code like so:
ArrayList<Map> z = (ArrayList) it.next().get("z");
for (Map m : z) {
// Run mapper on o.
doSomethingWith(m.get("stuff");
}
and everything works splendidly.

Related

Working with Jackson to convert nested JSON to Map

I'm currently using com.fasterxml.jackson.databind.ObjectMapper to take a JSON string and convert to a map. I'm struggling though to figure out how to work with my particular JSON input though and then how to Parse the output. The JSON I'm playing around with looks like this:
{
"data":[
{
"DATA.HELLO":[
{
"do_something":"true"
}
],
"DATA.GOODBYE":[
{
"do_something":"false"
}
]
}
]
}
Below is a snippet of my code. Ultimately I want something like a Map<String, Map> where I can grab "do_something" or whatever other attribute from "data.hello" or "data.goodbye".
So I can do something like map.get("data") to get the array, but I'm not sure how to grab - for example - the DATA.HELLO piece. I'm also not sure if what I'm doing here is the best way to handle this as this is the first time I've worked with Jackson and I'm fairly new to Java. Am I on the completely wrong track here?
String data = "{\"data\":[{\"DATA.HELLO\":[{\"do_something\":\"true\"}],\"DATA.GOODBYE\":[{\"do_something\":\"true\"}]}]}";
ObjectMapper mapper = new ObjectMapper();
Map<String, Map> map = mapper.readValue(data, Map.class);
System.out.println(data);

How to proccess/read java objects from xmlrpc received by a server

First, im learning java, so i am totally new with it, i am making a petition to a python function with xmlrpc, python sents a dictionary which contanins another dictionary inside, and various ids lists like this:
{
country_ids=[1,2,3,4,6,7,8],
state_ids=[23,22,12,12,56,12,56,72,23],
config={GLOBAL_DC=true, MAX_GLOBAL_DC=1,RET=5,COMP=1,VER=1.0}
}
So im getting this in java with:
HashMap<String, Object> data=HashMap<String, Object> xmlrpc.call...
and i am getting something like this:
{
country_ids=[Ljava.lang.Object;#7e0aa6f,
state_ids=[Ljava.lang.Object;#dc6c405,
config={GLOBAL_DC=true, MAX_GLOBAL_DC=1,RET=5,COMP=1,VER=1.0}
}
I know how to read value from the hashmap with data.get("country_ids") but, i don't know how to map/read/convert this object to get the ids inside of it.
Just in case someone cames here wondering the same or similar question, i found how has to be done:
Object [] country_ids = (Object[]) data.get('country_ids');
// To read the data of elements, something like this
for (Integer i = 0; i < country_ids.length; i++) {
Log.d("Element value of " + i.toString(),country_ids[i].toString());
}

JSON construction

I am new to JSON and getting confused everytime I create a new one.
I am trying to create a JSON array like this :
{
"id":"2003",
"HouseMD" :
{
"Doctor_1": "Thirteen",
"Doctor_2" : "Chase"
"Doctor_n" : "Someone"
}
}
Basically I am trying to add info dynamically from Doctor_1 to Doctor_n" in a for loop. and if I use a JSON Object I am only getting the last value when I finally print it.
How do I get something that I want?
Any help appreciated.
Thanks.
JSON arrays look like this:
{ "id":"2003", "HouseMD" : [{ "Doctor_1": "Thirteen"}, {"Doctor_2" : "Chase"}, {"Doctor_n" : "Someone" }]}
Notice the square bracket that surrounds each JSON object in the array.
Here is the link to the JSON website, which can offer more info:
JSON
Note that in order for the code below to work, you will also need the JSON library, which you can easily download from here Download Java JSON Library
I don't know the approach you are using, but based on the format you want, I would do something like this:
JSONObject data = new JSONObject();
data.put("id", "2003");
JSONObject doctors = new JSONObject();
//here I suppose you have all doctors in a list named doctorList
//and also suppose that you get the name of a doctor by the getName() method
for (int i = 0; i < doctorList.size(); ++i)
{
doctors.put("Doctor_" + (i+1), doctorList.get(i).getName();
}
data.put("HouseMD", doctors);
//then you could write to a file, or on screen just for test
System.out.println(data.toString());
However, I feel you need to become more comfortable with JSON, so try starting here.

create json object from other json objects

I have two json objects -- not strings, please -- that I want to combine into one json object as shown below.
The two objects:
JSONObject pen = {"plastic":"blue"}
JSONObject book = {"Maya":"Bird"}
Desired result:
JSONObject bag = {"plastic":"blue","Maya":"Bird"}
Is there an elegant way to do this? I mean without extracting the values of each pen and book and then re-insert them into bag using something like
bag.put("plastic","blue");
I am using org.codehaus.jettison.json.JSONObject if that information is necessary.
Naively, it seems like you could just do something like:
Iterator it = pen.keys();
while(it.hasNext())
{
String k = it.next();
bag.put(k, pen.getJSONObject(k));
}
// bag now has the combined key,value pairs.
But it has its obvious limitations.

How to convert string array of data object to array list of another data object in java

I have vehicle data object returning string array .I want to convert or cast to Arraylist BuyingDo data object.I have ArrayList objectList
I am getting value like this
VehicleDo list[]=data.getList()
I am converting like this
objectList=(ArrayList<BuyingDo)list
but its not working can anybody tell how to do this
Your question is a bit... well... unclear.
I think you mean either this:
List<VehicleDO> realList = Arrays.asList(list);
Or this:
List<BuyingDO> realList = new ArrayList<BuyingDO>(list.length);
for (VehicleDO vdo : list){
realList.add((BuyingDO)vdo);
}
Assuming of course that VehicleDO can be cast to BuyingDO.
Something like that?

Categories