简体   繁体   中英

Multiple values for a JSON key

I am a beginner on JSON in Java http://json.org/java/

How can I create a JSON object like this?

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

Try like this:

    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);

You have to make "RECORD" an JSONobject. This is an example:

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() );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM