使用java创建嵌套的json对象

时间:2014-07-10 10:21:01

标签: java json

我有一个示例JSON对象,我想用Java创建。如何使用son-“simple-1.1.1.jar”完成此操作?

{
 "name": "country",
 "children": [
  {
   "name": "USA",
   "children": [
    {
     "name": "person",
     "children": [
      {
       "name": "women",
       "children":[
         {"name":"sara", "age": 29},
         {"name":"jiya", "age": 75},
         {"name":"yena", "age": 43}
       ]
      },
      {
       "name": "men",
       "children":[
         {"name":"mickal", "age": 46},
         {"name":"joseph", "size": 45},
         {"name":"graham", "size": 43},
         {"name":"ronaldo", "size": 63}
       ]
      }
     ]
    },
    {
     "name": "india",
     "children": [
      {
       "name": "women",
       "children":[
         {"name":"priya", "size": 76},
         {"name":"sonal", "size": 75},
         {"name":"yena", "size": 73}
       ]
      },
      {
       "name": "men",
       "children":[
         {"name":"raj", "size": 46},
         {"name":"dev", "size": 45},
         {"name":"hari", "size": 43},
         {"name":"vel", "size": 63}
       ]
      }
     ]
    }
   ]
  }
 ]
}

1 个答案:

答案 0 :(得分:0)

很容易通过Jackson转换器从Java Object转换为JSON,反之亦然。请参阅以下代码         //杰克逊转换。         ObjectMapper mapper = new ObjectMapper();         书b3 = mapper.readValue(new File(“book.json”),Book.class); //从文件中读取json并转换为Book对象         System.out.println(“jackson b3 - ”+ b3);

    Book b1 = new Book(1, "Learn Java", d1, "James Gosling", 23.50);
    mapper.writeValue(new File("book.json"), b1);  // write java to json object file.

以下罐子是必需的     import org.codehaus.jackson。;     import org.codehaus.jackson.map。;