Convert JSON to Map

后端 未结 17 3104
天涯浪人
天涯浪人 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:17

    Underscore-java library can convert json string to hash map. I am the maintainer of the project.

    Code example:

    import com.github.underscore.lodash.U;
    import java.util.*;
    
    public class Main {
    
        @SuppressWarnings("unchecked")
        public static void main(String[] args) {
            String json = "{"
                + "    \"data\" :"
                + "    {"
                + "        \"field1\" : \"value1\","
                + "        \"field2\" : \"value2\""
                + "    }"
                + "}";
    
           Map data = (Map) U.get((Map) U.fromJson(json), "data");
           System.out.println(data);
    
           // {field1=value1, field2=value2}
        }
    }
    

提交回复
热议问题