【问题标题】:How to create JSON Object using String?如何使用字符串创建 JSON 对象?
【发布时间】:2013-12-05 16:44:23
【问题描述】:

我想使用 String 创建一个 JSON 对象。

示例: JSON{"test1":"value1","test2":{"id":0,"name":"testName"}}

为了创建上面的 JSON,我正在使用它。

String message;
JSONObject json = new JSONObject();

json.put("test1", "value1");

JSONObject jsonObj = new JSONObject();

jsonObj.put("id", 0);
jsonObj.put("name", "testName");
json.put("test2", jsonObj);

message = json.toString();
System.out.println(message);

我想知道如何创建一个包含 JSON 数组的 JSON。

以下是示例 JSON。

{
  "name": "student",
   "stu": {
    "id": 0,
    "batch": "batch@"
  },
  "course": [
    {
      "information": "test",
      "id": "3",
      "name": "course1"
    }
  ],
  "studentAddress": [
    {
      "additionalinfo": "test info",
      "Address": [
        {
          "H.No": "1243",
          "Name": "Temp Address",
          "locality": "Temp locality",
           "id":33          
        },
        {
           "H.No": "1243",
          "Name": "Temp Address",
          "locality": "Temp locality", 
           "id":33                   
        },        
        {
           "H.No": "1243",
          "Name": "Temp Address",
          "locality": "Temp locality", 
           "id":36                   
        }
      ],
"verified": true,
    }
  ]
}

谢谢。

【问题讨论】:

    标签: java android json


    【解决方案1】:

    JSONArray 可能是你想要的。

    String message;
    JSONObject json = new JSONObject();
    json.put("name", "student");
    
    JSONArray array = new JSONArray();
    JSONObject item = new JSONObject();
    item.put("information", "test");
    item.put("id", 3);
    item.put("name", "course1");
    array.put(item);
    
    json.put("course", array);
    
    message = json.toString();
    
    // message
    // {"course":[{"id":3,"information":"test","name":"course1"}],"name":"student"}
    

    【讨论】:

    • 如何将字符串转换回JSONObject
    • JSONObject jsonObj = new JSONObject("your_json_string");
    • 仅供参考,这将无法解析 JSON 数组(即使它们在技术上是有效的 JSON)。例如,尝试JSONObject("[{\"foo\":2, \"bar\": 3}]"); 会导致A JSONObject text must begin with '{' at 1 [character 2 line 1]
    【解决方案2】:

    与接受的答案建议相反,文档说对于 JSONArray(),您必须使用 put(value) 而不是 add(value)

    https://developer.android.com/reference/org/json/JSONArray.html#put(java.lang.Object)

    (Android API 19-27. Kotlin 1.2.50)

    【讨论】:

      【解决方案3】:

      如果你使用 gson.JsonObject 你可以有类似的东西:

      import com.google.gson.JsonObject;
      import com.google.gson.JsonParser;
      
      String jsonString = "{'test1':'value1','test2':{'id':0,'name':'testName'}}"
      JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonString)
      

      【讨论】:

      • 它对我有用,最简单的解决方案,谢谢
      【解决方案4】:

      Underscore-java 可以从对象创建 json。

          String message = U.objectBuilder()
                  .add("course", U.arrayBuilder()
                          .add(U.objectBuilder()
                                  .add("id", 3)
                                  .add("information", "test")
                                  .add("name", "course1")
                          ))
                  .add("name", "student")
                  .toJson();
          System.out.println(message);
      
      // {
      //   "course": [
      //     {
      //       "id": 3,
      //       "information": "test",
      //       "name": "course1"
      //     }
      //   ],
      //   "name": "student"
      // }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 2017-01-04
        • 2022-01-22
        • 2011-03-08
        • 1970-01-01
        相关资源
        最近更新 更多