Create key-value pairs string in JSON

前端 未结 3 826
渐次进展
渐次进展 2020-12-19 20:53

I\'m new to JSON. I\'m trying to create a JSON string in Java (org.json.JSONObject(json.jar)) which resembles like (basically a set of name-value pairs)

[{
          


        
3条回答
  •  粉色の甜心
    2020-12-19 21:17

    Try to use gson if you have to work a lot with JSON in java. Gson is a Java library that can be used to convert Java Objects into JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

    Here is a small example:

    Gson gson = new Gson();
    gson.toJson(1);            ==> prints 1
    gson.toJson("abcd");       ==> prints "abcd"
    gson.toJson(new Long(10)); ==> prints 10
    int[] values = { 1 };
    gson.toJson(values);       ==> prints [1]
    

提交回复
热议问题