Tag Archives: common problem

JSON and map convert to each other (using fastjson)

article directory

JSON String

    • to Map, get leaf node
    • Map to String String
  • JSON string to Map, get the leaf node

    fastjson has two ways:

    • json. parseObject (String, Class< T>)

    //JSON subclass

    • JSONObject.parseObject
    String str ={
     "sign": "sign",
     "data": {
      "type": "第二层",
      "order": {
       "test": "第三层"
      }
     }
    }
    
    Map<String,String> parseObject = JSON.parseObject(str,Map.class);
    	//获取sign节点
    String sign=parseObject.get("sign");
    	//获取data节点
    String data=String.valueOf(parseObject.get("data"));
    	//获取data集合
    Map dataMap  = JSON.parseObject(data,Map.class);
    	//获取order集合
    Map orderMap = (Map)dataMap.get("order"); 
    	//获取叶子节点
    orderMap.get("test");
    

    Map String String

    String str = JSONObject.toJSONString(testMap);
    

    – tools