22

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()));
2
  • 1
    Do you try to communicate with service using json? Do you realy need to send so dificult structure? Commented Mar 29, 2012 at 14:37
  • 8
    Are you kidding me? WHAT is so difficult with sending a nested JSON object!? Commented Mar 31, 2012 at 17:46

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I meant nested instead of multidimensional. :)
@Ovidiu Latcu Please Check my question stackoverflow.com/questions/15171454/…
@OvidiuLatcu can you please check my question [stackoverflow.com/questions/42024158/…
1

Create a Model Class for the Json, initialize it with your data. Then use Google Gson to convert it toJson also from Json to Back to the Object. and also here are two links which make your life more Easier for using Json. Online Json Editor. You can check that generated json is in the correct format or not. http://www.jsoneditoronline.org/ And Json to POJO Generated (Model Class Generator) http://www.jsonschema2pojo.org/

Comments

1

BasicNameValuePair is what you use to add POST parameters. It's a little unclear what you are asking. are you asking how, given that JSON, to submit the param values as post parameters?

You need to first parse the JSON, then pull out the parameters, then add them to the form post, like this,

JSONObject jo = new JSONObject(jsonString);
JSONObject joParams = jo.getJSONObject("params");
String username = joParams.getString("username");

post.add(new BasicNameValuePair("username",username);

You will of course want to do checking on the JSON to ensure the params object exists, and to ensure the username parameter exits.

Comments

0

If you just want to send a block of JSON to the server you should not use key value pairs but just post the data as a StringEntity.

    HttpPost request = new HttpPost(api_call);

    request.setHeader("Content-Type", "text/javascript");
    request.setEntity(new StringEntity(jsonString);

    httpClient.execute(request);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.