A
JSONObject is an unordered collection of name/value pairs. It is used to
create json document.
For
example, lets create below json document.
{
"organization": "ABC
Corportion",
"name": "Krishna",
"id": 123
}
Step 1: Instantiate JsonObject
JSONObject
jsonObject = new JSONObject();
Step 2: Add respective properties using put
method.
jsonObject.put("name",
"Krishna");
jsonObject.put("id",
123);
jsonObject.put("organization",
"ABC Corportion");
App.java
package com.sample; import org.json.JSONObject; public class App { public static void main(String args[]) { JSONObject jsonObject = new JSONObject(); jsonObject.put("name", "Krishna"); jsonObject.put("id", 123); jsonObject.put("organization", "ABC Corportion"); System.out.println(jsonObject); } }
Output
{"organization":"ABC
Corportion","name":"Krishna","id":123}
Note
a.
Keys
are unique strings that can’t be null
b.
Values
can be any primitive types, JSONArray or JSONObject.NULL.
No comments:
Post a Comment