I have a JSON structure as given below. How can I create this Array structure programmatically?
JSON srtucture:
{
"Employee":
[
{
"EmP325235":
{
"Name":"Rekha_S",
"firstName":"Rekha",
"phoneWork":"788908909",
"lastName":"S",
"namePrefix":"Miss",
"phoneMobile":"3347687878",
"phoneHome":"5768900909",
"Email":""
},
"Em252555":
{
"Name":"Roopa_N",
"firstName":"Roopa",
"phoneWork":"0471245367",
"lastName":"N",
"namePrefix":"",
"phoneMobile":"",
"phoneHome":"",
"Email":""
}
}
],
"User_Details":
{
"USER_ID":"7890",
"Number":"8585858585",
"Password":"Passwordsgs"
}
}
jsonObject. put("key", "value"); Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class.
JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object.
Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.
' { } ' used for Object and ' [] ' is used for Array in json.
I'd probably use the GSON library which will marshall Java objects.
There's an array example to get you started, as well as a ton of others in the user guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With