Convert JSON to Map

后端 未结 17 3102
天涯浪人
天涯浪人 2020-11-22 10:20

What is the best way to convert a JSON code as this:

{ 
    \"data\" : 
    { 
        \"field1\" : \"value1\", 
        \"field2\" : \"value2\"
    }
}
         


        
17条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 11:14

    I hope you were joking about writing your own parser. :-)

    For such a simple mapping, most tools from http://json.org (section java) would work. For one of them (Jackson https://github.com/FasterXML/jackson-databind/#5-minute-tutorial-streaming-parser-generator), you'd do:

    Map result =
            new ObjectMapper().readValue(JSON_SOURCE, HashMap.class);
    

    (where JSON_SOURCE is a File, input stream, reader, or json content String)

提交回复
热议问题