日期:2014-05-20  浏览次数:20678 次

怎么用JAVA生成下面的JSON字符串,
本帖最后由 eimhee 于 2013-08-09 21:44:40 编辑
怎么用JAVA生成下面的JSON字符串,
我知道能用JSON 的LIB生成,
但如果先生成HASHMAP, 再把HASHMAP转成JSON,
要很多步, HASHMAP里放HASHMAP,又放LIST才能生成,
有没有简单的办法,最好能帮忙写出代码


            ObjectMapper mapper = new ObjectMapper();
            
            Map<String, Object> userInMap = new HashMap<String, Object>();
            
            userInMap.put("title", titile);
            Map<String, Object> bodyMap = new HashMap<String, Object>();
            bodyMap.put("und", "");
            userInMap.put("body", bodyMap);
            
            jsonStr = mapper.writeValueAsString(userInMap);



{
   "title":"Review",
   "type":"establishment_review",
   "field_establishment":[
      {
         "nid":{
            "nid":"[nid:26686]"
         }
      }
   ],
   "field_fivestar_value":[
      {
         "rating":"20",
         "target":"0"
      }
   ],
   "field_review":[
      {
         "value":"comments about the restaurant"
      }
   ]
}