如何在java中解析包含数组和值的json树中的JSON值

时间:2017-06-27 11:17:11

标签: java json

我正在编写一个程序,通过发送请求从服务器获取数据。 所有值都是JSON格式。

我无法理解JSONobject是否包含JSONArray或JSONArray是否包含对象。

我的数据采用以下格式:

{
    "items": [
        {
            "name": "accounts",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/accounts"
                }
            ]
        },
        {
            "name": "analyticsReports",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/analyticsReports"
                }
            ]
        },
        {
            "name": "answers",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/answers"
                }
            ]
        },
        {
            "name": "assets",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/assets"
                }
            ]
        },
        {
            "name": "assetStatuses",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/assetStatuses"
                }
            ]
        },
        {
            "name": "channelTypes",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/channelTypes"
                }
            ]
        },
        {
            "name": "configurations",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/configurations"
                }
            ]
        },
        {
            "name": "contacts",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/contacts"
                }
            ]
        },
        {
            "name": "countries",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/countries"
                }
            ]
        },
        {
            "name": "eventSubscriptions",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/eventSubscriptions"
                }
            ]
        },
        {
            "name": "holidays",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/holidays"
                }
            ]
        },
        {
            "name": "incidents",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/incidents"
                }
            ]
        },
        {
            "name": "mailboxes",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/mailboxes"
                }
            ]
        },
        {
            "name": "marketingMailboxes",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/marketingMailboxes"
                }
            ]
        },
        {
            "name": "messageBases",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/messageBases"
                }
            ]
        },
        {
            "name": "opportunities",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/opportunities"
                }
            ]
        },
        {
            "name": "organizations",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/organizations"
                }
            ]
        },
        {
            "name": "purchasedProducts",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/purchasedProducts"
                }
            ]
        },
        {
            "name": "salesProducts",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/salesProducts"
                }
            ]
        },
        {
            "name": "salesTerritories",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/salesTerritories"
                }
            ]
        },
        {
            "name": "serviceCategories",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/serviceCategories"
                }
            ]
        },
        {
            "name": "serviceDispositions",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/serviceDispositions"
                }
            ]
        },
        {
            "name": "serviceMailboxes",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/serviceMailboxes"
                }
            ]
        },
        {
            "name": "serviceProducts",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/serviceProducts"
                }
            ]
        },
        {
            "name": "siteInterfaces",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/siteInterfaces"
                }
            ]
        },
        {
            "name": "sSOTokenReferences",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/sSOTokenReferences"
                }
            ]
        },
        {
            "name": "standardContents",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/standardContents"
                }
            ]
        },
        {
            "name": "tasks",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/tasks"
                }
            ]
        },
        {
            "name": "variables",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/variables"
                }
            ]
        },
        {
            "name": "Asset.TypeOfInstallation",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Asset.TypeOfInstallation"
                }
            ]
        },
        {
            "name": "Asset.AssetType",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Asset.AssetType"
                }
            ]
        },
        {
            "name": "Asset.AMC_Warranty",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Asset.AMC_Warranty"
                }
            ]
        },
        {
            "name": "Asset.Vendor",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Asset.Vendor"
                }
            ]
        },
        {
            "name": "BOB.BOB",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/BOB.BOB"
                }
            ]
        },
        {
            "name": "BOB.Beverages_Name",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/BOB.Beverages_Name"
                }
            ]
        },
        {
            "name": "BOB.Supply_Name",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/BOB.Supply_Name"
                }
            ]
        },
        {
            "name": "BOB.Recipes",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/BOB.Recipes"
                }
            ]
        },
        {
            "name": "BOB.UOM",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/BOB.UOM"
                }
            ]
        },
        {
            "name": "BOM.Bill_On_Material",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/BOM.Bill_On_Material"
                }
            ]
        },
        {
            "name": "BOS.Bill_On_Supply",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/BOS.Bill_On_Supply"
                }
            ]
        },
        {
            "name": "Depreciate.Depreciation",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Depreciate.Depreciation"
                }
            ]
        },
        {
            "name": "Depreciate.Operator_Cost",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Depreciate.Operator_Cost"
                }
            ]
        },
        {
            "name": "Site_Inspec.Site_Status_Install",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Site_Inspec.Site_Status_Install"
                }
            ]
        },
        {
            "name": "Spares.Spares",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Spares.Spares"
                }
            ]
        },
        {
            "name": "Spares.Spares_Consumption",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Spares.Spares_Consumption"
                }
            ]
        },
        {
            "name": "Spares.Item_Category",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Spares.Item_Category"
                }
            ]
        },
        {
            "name": "Spares.Item_Request",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Spares.Item_Request"
                }
            ]
        },
        {
            "name": "Spares.Spares_Request",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/Spares.Spares_Request"
                }
            ]
        },
        {
            "name": "queryResults",
            "links": [
                {
                    "rel": "canonical",
                    "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest/queryResults"
                }
            ]
        }
    ],
    "links": [
        {
            "rel": "self",
            "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest"
        },
        {
            "rel": "canonical",
            "href": "https://lavazzaindia--tst1.custhelp.com/services/rest/connect/latest"
        }
    ]
}

我使用JSONEditor来理解这些数据,2 JSON Objects名为itemslinks

这两个对象包含JSONArray。

我使用以下循环获取java程序中的所有值:

         String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

这里我收到output数据显示但是我需要在JSON对象中输出。

我应该在while循环中做什么,以便我可以将JSONObject作为我的对象。

请有人帮助我,我如何将这些JSON数据解析为单独的对象。我在这里完全糊涂了。

我在哪里以及如何从JSONArray,JSONObject或String转换?

0 个答案:

没有答案