在JAVA中遍历JSON数据 [英] Traverse JSON data in JAVA

查看:3034
本文介绍了在JAVA中遍历JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSON的新手..Am使用HTTPUrlConnections并在JAVA程序中获得了一些响应.响应数据将类似于

I am new to JSON..Am using HTTPUrlConnections and getting some response in JAVA program.The response data will be like,

    {
    "data": [
        {
    "id": 1,
            "userId": 1,
            "name": "ABC",
            "modified": "2014-12-04",
            "created": "2014-12-04",
            "items": [
                {
                    "email": "abc@gmail.com",
                    "links": [
                        {
                            .
                            .
                            .
                            .
                        }
                    ]
                }
            ]
            }
        ]
}

通过此响应,可以使用以下Java代码获取"名称"字段的值.

From this response am able to get the value of "name" field with the below java code.

JSONArray items = newObj.getJSONArray("data");
for (int it=0 ; it < items.length() ; it++){
    JSONObject contactItem = items.getJSONObject(it);
    String userName = contactItem.getString("name");
    System.out.println("Name----------"+userName);
}

但是我的要求是,我需要获取"电子邮件"的值. 任何建议.

But my requirement is,I need to get the value of "email" ..How should I code for that.. Any advice..

谢谢. Chitra

Thanks in advance.. Chitra

推荐答案

您需要首先获取items数组,并且该数组的每个条目都包含JSONObject,您可以从中调用getString("email") .Eg

You need to first get the items array and each entry of this array contains JSONObject, from which you can call getString("email") .E.g.

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class App
{

    public static void main( String[] args ) throws JSONException {
        JSONObject newObj = new JSONObject("{" +
                "\"data\": [\n" +
                "    {\n" +
                "\"id\": 1,\n" +
                "        \"userId\": 1,\n" +
                "        \"name\": \"ABC\",\n" +
                "        \"modified\": \"2014-12-04\",\n" +
                "        \"created\": \"2014-12-04\",\n" +
                "        \"items\": [\n" +
                "            {\n" +
                "                \"email\": \"abc@gmail.com\",\n" +
                "                \"links\": [\n" +
                "                    {\n" +

                "                    }\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "        }\n" +
                "    ]\n" +
                "\n" +
                "}");



        JSONArray items = newObj.getJSONArray("data");
        for (int it = 0; it < items.length(); it++) {
            JSONObject contactItem = items.getJSONObject(it);
            String userName = contactItem.getString("name");


            JSONArray item = contactItem.getJSONArray("items");

            for (int i = 0; i < items.length(); i++) {
                String email = item.getJSONObject(i).getString("email");
                System.out.println(email);
            }

            System.out.println("Name----------" + userName);
        }
    }
}

输出

abc@gmail.com
Name----------ABC

这篇关于在JAVA中遍历JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆