How to convert json map object to array of objects with jackson library

I have next json:

{
    "blabla": {
        "1": {
            "foo": "foo1",
            "bar": "bar1"
        },
        "22": {
            "foo": "foo22",
            "bar": "bar22"
        }
    }
}

It is map of int to object. Is there a way to convert it to array of objects like:

class FooBar
{
    public int id;
    public String foo;
    public String bar;
}

without parsing as map and iterating over it to convert?

Answers


According to your pasted json as it is, I can assume class structure like below (...yes, your json can't parse as your FooBar class as it is. Because json indicate id as a key, but FooBar class contains id as a member variable. So, they can't be converted each other.) :

class FooBar {                                             
    public String foo;                                     
    public String bar;                                     
};                                                         

class FooBarObject {                                       
    public HashMap<String, FooBar> blabla;                 
    public void setBlabla(HashMap<String, FooBar> blabla) {
        this.blabla = blabla;                              
    }                                                      
    public HashMap<String, FooBar> getBlabla() {           
        return blabla;                                     
    }                                                      
};                                                         

And, this is sample code.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Hello world!
 */
class FooBar {
    public String foo;
    public String bar;
};

class FooBarObject {
    public HashMap<String, FooBar> blabla;
    public void setBlabla(HashMap<String, FooBar> blabla) {
    this.blabla = blabla;
    }
    public HashMap<String, FooBar> getBlabla() {
    return blabla;
    }
};

public class App {

    static String target = ""
    + "{"
    + "    \"blabla\": {"
    + "        \"1\": {"
    + "            \"foo\": \"foo1\","
    + "            \"bar\": \"bar1\""
    + "        },"
    + "        \"22\": {"
    + "            \"foo\": \"foo22\","
    + "            \"bar\": \"bar22\""
    + "        }"
    + "    }"
    + "}";

    public static void main(String[] args) throws Exception {

    FooBarObject blabla = new FooBarObject();
    HashMap map = new HashMap<String, FooBar>();
    map.put("1", new FooBar());
    map.put("22", new FooBar());
    blabla.setBlabla(map);

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(blabla);

    System.out.println("--- BEGIN ---");
        System.out.println(json);
    System.out.println("--- END ---");

    blabla = mapper.readValue(target, FooBarObject.class);
    System.out.println("--- BEGIN ---");
    for ( Map.Entry<String, FooBar> entry : blabla.blabla.entrySet() ) {
        String internal = mapper.writeValueAsString(entry.getValue());
        System.out.println(entry.getKey() + ": " + internal);
    }
    System.out.println("--- END ---");
    }
}

But, I guess there is already something wrong...Do you actually want to use Array ?

{
    "blabla": [
        "1": {
            "foo": "foo1",
            "bar": "bar1"
        },
        "22": {
            "foo": "foo22",
            "bar": "bar22"
        }
    ]
}

Need Your Help

Scala convert Seq to k, v of sbt.Project.Setting[_]

scala build playframework sbt seq

Play 2.0's Build.sbt uses a pimped Project definition to do its magic; any additional settings you might need to add must be manually entered in k := v fashion. Works fine for the general case, but...

how divide NSMutableDictionary into two NSMutableArrays?

iphone objective-c nsmutablearray nsmutabledictionary

I am trying to divide a NSMutableDictionary into two NSMutableArrays on comparing one keyValue in NSMutableDictionary,