Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create JSON Array in Java

Tags:

java

json

arrays

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" 
   } 
}
like image 328
jennifer Avatar asked Nov 02 '11 06:11

jennifer


People also ask

How do you create an array of objects in JSON in Java?

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.

What is JSON array in Java?

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.

Is it possible to create arrays in JSON?

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.

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.


1 Answers

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.

like image 53
Dave Newton Avatar answered Oct 26 '22 13:10

Dave Newton