Converting JSON data to Java object

后端 未结 12 1082
春和景丽
春和景丽 2020-11-21 05:11

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson()

相关标签:
12条回答
  • 2020-11-21 05:36

    If, by any change, you are in an application which already uses http://restfb.com/ then you can do:

    import com.restfb.json.JsonObject;
    
    ...
    
    JsonObject json = new JsonObject(jsonString);
    json.get("title");
    

    etc.

    0 讨论(0)
  • 2020-11-21 05:37
    HashMap keyArrayList = new HashMap();
    Iterator itr = yourJson.keys();
    while (itr.hasNext())
    {
        String key = (String) itr.next();
        keyArrayList.put(key, yourJson.get(key).toString());
    }
    
    0 讨论(0)
  • 2020-11-21 05:37

    If you use any kind of special maps with keys or values also of special maps, you will find it's not contemplated by the implementation of google.

    0 讨论(0)
  • 2020-11-21 05:38

    I looked at Google's Gson as a potential JSON plugin. Can anyone offer some form of guidance as to how I can generate Java from this JSON string?

    Google Gson supports generics and nested beans. The [] in JSON represents an array and should map to a Java collection such as List or just a plain Java array. The {} in JSON represents an object and should map to a Java Map or just some JavaBean class.

    You have a JSON object with several properties of which the groups property represents an array of nested objects of the very same type. This can be parsed with Gson the following way:

    package com.stackoverflow.q1688099;
    
    import java.util.List;
    import com.google.gson.Gson;
    
    public class Test {
    
        public static void main(String... args) throws Exception {
            String json = 
                "{"
                    + "'title': 'Computing and Information systems',"
                    + "'id' : 1,"
                    + "'children' : 'true',"
                    + "'groups' : [{"
                        + "'title' : 'Level one CIS',"
                        + "'id' : 2,"
                        + "'children' : 'true',"
                        + "'groups' : [{"
                            + "'title' : 'Intro To Computing and Internet',"
                            + "'id' : 3,"
                            + "'children': 'false',"
                            + "'groups':[]"
                        + "}]" 
                    + "}]"
                + "}";
    
            // Now do the magic.
            Data data = new Gson().fromJson(json, Data.class);
    
            // Show it.
            System.out.println(data);
        }
    
    }
    
    class Data {
        private String title;
        private Long id;
        private Boolean children;
        private List<Data> groups;
    
        public String getTitle() { return title; }
        public Long getId() { return id; }
        public Boolean getChildren() { return children; }
        public List<Data> getGroups() { return groups; }
    
        public void setTitle(String title) { this.title = title; }
        public void setId(Long id) { this.id = id; }
        public void setChildren(Boolean children) { this.children = children; }
        public void setGroups(List<Data> groups) { this.groups = groups; }
        
        public String toString() {
            return String.format("title:%s,id:%d,children:%s,groups:%s", title, id, children, groups);
        }
    }
    

    Fairly simple, isn't it? Just have a suitable JavaBean and call Gson#fromJson().

    See also:

    • Json.org - Introduction to JSON
    • Gson User Guide - Introduction to Gson
    0 讨论(0)
  • 2020-11-21 05:38

    Or with Jackson:

    String json = "...
    ObjectMapper m = new ObjectMapper();
    Set<Product> products = m.readValue(json, new TypeReference<Set<Product>>() {});
    
    0 讨论(0)
  • 2020-11-21 05:39

    Depending on the input JSON format(string/file) create a jSONString. Sample Message class object corresponding to JSON can be obtained as below:

    Message msgFromJSON = new ObjectMapper().readValue(jSONString, Message.class);
    
    0 讨论(0)
提交回复
热议问题