Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a nested Json object in Java?

Tags:

I want to program a nested JSON Object for my Android device, but how? I have only basic experience with Json, so I would appreciate if someone could help me with the following code:

{   "command": "login",  "uid": "123123123",  "params":   {    "username": "sup",    "password": "bros"   } } 

Edit:

The following code does solve the problem:

loginInformation = new ArrayList<NameValuePair>();   JSONObject parentData = new JSONObject(); JSONObject childData = new JSONObject();  try {      parentData.put("command", "login");     parentData.put("uid", UID.getDeviceId());      childData.put("username", username.getText().toString());     childData.put("password", password.getText().toString());     parentData.put("params", childData);  } catch (JSONException e) {     e.printStackTrace(); }  loginInformation.add(new BasicNameValuePair("content", parentData.toString())); 
like image 307
Aerial Avatar asked Mar 29 '12 14:03

Aerial


People also ask

Can JSON objects be nested?

Objects can be nested inside other objects. Each nested object must have a unique access path. The same field name can occur in nested objects in the same document.

What is JSON nested?

Nested JSON is a JSON file with a big portion of its values being other JSON objects.

Can JSON arrays be nested?

Attributes and event data can contain nested (JSON) values—arrays, objects, and arrays of objects. You can use these nested values to match people in segments, filters, and trigger criteria.


1 Answers

You need to create a JSON, and add that JSON as a parameter in your POST. To do that you should probably :

post.add(new BasicNameValuePair("data",json.toString()); 

You could use org.json.JSONObject, it is included in the Android SDK.

like image 141
Ovidiu Latcu Avatar answered Sep 22 '22 05:09

Ovidiu Latcu