2

我是 Java 中 JSON 的初学者http://json.org/java/

如何创建这样的 JSON 对象?

{
    "RECORD": {
        "customer_name": "ABC",
        "customer_type": "music"
    }
}
4

2 回答 2

2

试试这样:

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("customer_name", "ABC");
    jsonObject.put("customer_type", "music");
    JSONObject jsonObject_rec = new JSONObject();
    jsonObject_rec.put("RECORD", jsonObject);
    System.out.println(jsonObject_rec);
于 2013-10-31T05:17:55.850 回答
1

您必须使“记录”成为 JSON 对象。这是一个例子:

JSONObject json = new JSONObject();

    // Add a JSON Object
    JSONObject Record = new JSONObject();
    Record.put( "customer_name", "ABC");
    Record.put( "customer_type", "music");
    json.put( "RECORD", Record);


    // P toString() 
    System.out.println( "JSON: " + json.toString() );
于 2013-10-31T05:31:28.333 回答