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

标签 android json

我想为我的 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/

相关文章:

android - 如何在警告对话框中放置搜索栏?

java - 安卓 TreeView

java - IOI Activity 和 AppCompatActivity

java - 添加特定代码行后,Android 应用程序不断崩溃

javascript - jQuery AJAX 跨域

android - 无论屏幕设备分辨率如何,都将按钮控件设置为适合屏幕

javascript - 无法读取 JSON - 无法读取未定义的属性

json - 在Go中为map[string]interface{}创建方法

c# - 调整 jsonSerializer 时区

java - 查询数据库并以 Json 格式返回结果的通用 Java 方法