java - java中将json转换为字符串

标签 java android json string

如何将复杂的 json 对象放入字符串数组中?

JSON 示例

{
  "FriendsList": [
    {
      "username": "ChanServ",
      "avatar": "http:\/\/clavatars.photos-box.org\/chack.jpg"
    },
    {
      "username": "kroot",
      "avatar": "http:\/\/clavatars.photos-box.org\/land2.jpg"
    },
    {
      "username": "morrildl",
      "avatar": "http:\/\/clavatars.photos-box.org\/land3.jpg"
    },
    {
      "username": "addo",
      "avatar": "http:\/\/clavatars.photos-box.org\/land8.jpg"
    }
  ]
}

这是代码:

protected void onPostExecute(String result) {
    super.onPostExecute(result);
    JSONObject json;
    try {
        json = new JSONObject(result);
        tvhttp.setText(json.toString(2));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

最佳答案

试试这个

JSONObject json = null;
    try {
        json = new JSONObject(result);
        JSONArray jsonArray = json.getJSONArray("FriendsList");

        for (int j = 0; j < jsonArray.length(); j++) {

            JSONObject jsonObjectData1 = jsonArray.getJSONObject(j);

            String username = jsonObjectData1.getString("username");
            String avatar = jsonObjectData1.getString("avatar");
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

关于java - java中将json转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21479713/

相关文章:

android - 我的 android 应用程序在不同的设备上崩溃

javascript - 如果源 json 更新(添加或删除项目),则动态更新 D3 Sunburst

java - 托管 HIPPO CMS

java - Android - GregorianCalendar 显示错误的月份

android - 可扩展列表适配器根本不刷新

ios - 使用 JSON 数据填充 UILabel 并编码 URL

javascript - 无法获取 json 中的 js 数组

java - 将 "select foo from Foo foo, Bar bar where ..."形式的HQL查询转换为Criteria查询

java - 检查java中的多个 boolean 条件

java - 在 while 循环中嵌套 for 循环 - Java