我想为我的 Android 设备编写一个嵌套的 JSON 对象,但是怎么做?我只有 Json 的基本经验,所以如果有人可以帮助我使用以下代码,我将不胜感激:
{
"command": "login",
"uid": "123123123",
"params":
{
"username": "sup",
"password": "bros"
}
}
编辑:
下面的代码确实解决了这个问题:
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()));
最佳答案
您需要创建一个 JSON
,并将该 JSON
作为参数添加到您的 POST
中。为此,您可能应该:
post.add(new BasicNameValuePair("data",json.toString());
您可以使用 org.json.JSONObject
,它包含在 Android SDK
中。
关于android - 如何在 Java 中创建嵌套的 Json 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9927514/