3

以下の形式の JSON データがあります。「Non Veg」で specialDeal が true になっている店舗に対応する店舗名を取得する必要があります。(注: specialDeal が true になっている店舗は 1 つだけです) どうすればそれを取得できますか? 助けてください。前もって感謝します..私はJavaを学んでいるので助けてください。

    {"Food":
      {
       "Veg":
        {
            "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"", 
            "discntType":"", "Store":"", "StoreType":"", "Fund":"",
            "FundDetails":[
                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
            ]
         },
       "Non Veg":
            {
            "chicken":
                    [
                    {
                            "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                            "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                            "specialDeal":"", "promoStatus":"",
                            "FundDetails":[
                                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
                            ]
         },
         {
                            "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                            "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                            "specialDeal":"", "promoStatus":"",
                            "FundDetails":[
                                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
                            ]
         }
        ],
            "fish":
                    [
                    {
                            "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                            "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                            "specialDeal":"", "promoStatus":"",
                            "FundDetails":[
                                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
                            ]
                    },
                    {
                            "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                            "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                            "specialDeal":"", "promoStatus":"",
                            "FundDetails":[
                                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""},
                                    {"status":"", "discntVal":"", "FundVal":"", "FundBal":""}
                            ]
                    }
                    ],
            "egg":
                    [
                    {
                            "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                            "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                            "specialDeal":"", "promoStatus":"",
                            "discntVal":"", "FundVal":"", "FundBal":""
                    },
                    {
                            "amtSpent":"", "shortDesc":"", "longDesc":"", "excTxt":"",
                            "discntType":"", "Store":"", "StoreType":"", "Fund":"",
                            "specialDeal":"", "promoStatus":"",
                            "discntVal":"", "FundVal":"", "FundBal":""
                    }
                             ]
            },
    "isMember":"Y",
    "orderId":""
}

}

4

2 に答える 2

0

javascriptを想定:

野菜に関して、データにわずかな矛盾があります。次のように再構築することをお勧めします。

var test = { "Food":
{
    "Veg":
            {
                "vegetables":
                    [
                    {
                        "amtSpent": "", "shortDesc": "", "longDesc": "", "excTxt": "",
                        "discntType": "", "Store": "", "StoreType": "", "Fund": "",
                        "FundDetails": [
                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" },
                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" }
                            ]
                    }]
            },
    "Non Veg":
            {
                "chicken":
                    [
                    {
                        "amtSpent": "", "shortDesc": "", "longDesc": "", "excTxt": "",
                        "discntType": "", "Store": "", "StoreType": "", "Fund": "",
                        "specialDeal": "", "promoStatus": "",
                        "FundDetails": [
                                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" },
                                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" }
                            ]
                    },
                    {
                        "amtSpent": "", "shortDesc": "", "longDesc": "", "excTxt": "",
                        "discntType": "", "Store": "", "StoreType": "", "Fund": "",
                        "specialDeal": "", "promoStatus": "",
                        "FundDetails": [
                                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" },
                                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" }
                            ]
         }
        ],
                "fish":
                    [
                    {
                        "amtSpent": "", "shortDesc": "", "longDesc": "", "excTxt": "",
                        "discntType": "", "Store": "", "StoreType": "", "Fund": "",
                        "specialDeal": "", "promoStatus": "",
                        "FundDetails": [
                                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" },
                                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" }
                            ]
                    },
                    {
                        "amtSpent": "", "shortDesc": "", "longDesc": "", "excTxt": "",
                        "discntType": "", "Store": "", "StoreType": "", "Fund": "",
                        "specialDeal": "", "promoStatus": "",
                        "FundDetails": [
                                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" },
                                    { "status": "", "discntVal": "", "FundVal": "", "FundBal": "" }
                            ]
                    }
                    ],
                "egg":
                    [
                    {
                        "amtSpent": "", "shortDesc": "", "longDesc": "", "excTxt": "",
                        "discntType": "", "Store": "", "StoreType": "", "Fund": "",
                        "specialDeal": "", "promoStatus": "",
                        "discntVal": "", "FundVal": "", "FundBal": ""
                    },
                    {
                        "amtSpent": "", "shortDesc": "", "longDesc": "", "excTxt": "",
                        "discntType": "", "Store": "", "StoreType": "", "Fund": "",
                        "specialDeal": "", "promoStatus": "",
                        "discntVal": "", "FundVal": "", "FundBal": ""
                    }
                             ]
            },
    "isMember": "Y",
    "orderId": ""
}
}

specialDeal次に、次のように、プロパティを持つすべてのオブジェクトを取得できます。

for (key in test.Food) {
    if (test.Food.hasOwnProperty(key)) {
        var f = test.Food[key];
        for (cat in f) {
            if (f.hasOwnProperty(cat)) {
                for (prop in f[cat]) {
                    if (f[cat].hasOwnProperty(prop) && f[cat][prop].specialDeal != undefined) {
                        console.log(f[cat][prop].specialDeal, f[cat][prop], "Category: " + cat + ", Index: " + prop)
                    }
                }
            }
        }
    }
}

きれいではありませんが、機能します。

ここで行っているのは、オブジェクトをループしているということです。 for (key in test.Food) {}内のすべてのキーを検索しますtest.Food。それらは次のとおりです。

Veg
Non Veg
isMember
orderId

次に、を使用してサブオブジェクトを取得し、test.Food[key]そのオブジェクトを再度ループできます。ループのさらに下では、オブジェクトにspecialDealプロパティがあるかどうかをテストしています。その場合は、ログに記録します。

オブジェクトの問題は、特定の値を持つオブジェクトをオブジェクトで検索する場合、オブジェクトは通常のフォーマットである必要があることです。そのため、にオブジェクトを追加しました"Veg"

于 2012-11-22T13:31:20.550 に答える
0

1 つの方法は、これに JSONPath を使用することです。たとえば、次のような Java 実装を使用します。

List<Object> objectsHavingSpecialDeal = JsonPath.read(json, "$..*[?(@.specialDeal == 'true')]");
// iterate over that list to get the `Store` field value
for (Object o: objectsHavingSpecialDeal) {
    Map<String, String> fields = (Map<String, String>)o;
    System.out.println("Store: " + fields.get("Store"));
}
于 2012-11-22T13:13:15.090 に答える