如何在Java中创建嵌套的Json对象?

Xar*_*lon 18 android json

我想为我的Android设备编写嵌套的JSON对象,但是如何?我只有Json的基本经验,所以如果有人可以帮助我使用以下代码,我将不胜感激:

{ 
 "command": "login",
 "uid": "123123123",
 "params":
  {
   "username": "sup",
   "password": "bros"
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

以下代码确实解决了这个问题:

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()));
Run Code Online (Sandbox Code Playgroud)

Ovi*_*tcu 5

您需要创建一个JSON,并将其JSON作为参数添加到您的POST.要做到这一点你应该:

post.add(new BasicNameValuePair("data",json.toString());
Run Code Online (Sandbox Code Playgroud)

你可以使用org.json.JSONObject,它包含在Android SDK.