C#store app获取变量Json数据

时间:2014-05-28 08:10:26

标签: c# json windows-store-apps

我有一个JSON文件,如下所示构建。 JSON文件很好(用JSON检查器检查)问题是"特征" "标题"和"价值"如下所示,特征可以变化(我将一个特征完全不合逻辑,只是为了明确它真的可以是任何东西)。

这是我提取数据的C#代码: 旁注:它的测试没有特性而且有效。问题是我不知道"标题" (左值)一个特征可以是,它确实可以是任何东西。那么我怎么能在不知道它的价值的情况下从文件中提取特征呢?

编辑:我需要""之间的值,也许你知道更好的方法吗?

foreach (JsonValue groupValue in jsonArray)
        {

            JsonObject groupObject = groupValue.GetObject();
            ProductGroup group = new ProductGroup(groupObject["UniqueId"].GetString(),
                                                        groupObject["Title"].GetString(),
                                                        groupObject["ImagePath"].GetString());

            foreach (JsonValue itemValue in groupObject["Items"].GetArray())
            {

                JsonObject itemObject = itemValue.GetObject();
                ProductItem product = new ProductItem(itemObject["ItemNo"].GetString(),
                                                itemObject["Description"].GetString(),
                                                itemObject["UnitOfMeasure"].GetString(),
                                                itemObject["Delivery"].GetString(),
                                                Convert.ToInt32(itemObject["InPurchaseOrder"].GetString()),
                                                Convert.ToInt32(itemObject["InSalesOrder"].GetString()),
                                                itemObject["ItemBlocked"].GetString(),
                                                itemObject["ItemStatus"].GetString(),
                                                itemObject["UnitPriceExclVAT"].GetString(),
                                                itemObject["PurchasePrice"].GetString(),
                                                itemObject["Margin"].GetString(),
                                                Convert.ToInt32(itemObject["ActualStock"].GetString()),
                                                itemObject["ImagePath"].GetString(),
                                                Convert.ToBoolean(itemObject["IsActivated"].GetString()));

                foreach (JsonValue characteristicValue in itemObject["Characteristics"].GetArray()){
                    JsonObject characteristicObject = characteristicValue.GetObject();
                    ItemCharacteristic characteristic = new ItemCharacteristic(characteristicObject[""].GetString(),
                                                                                                    characteristicObject[""].GetString())
                }

Json数据:

{"Groups":[
{
"UniqueId": "Group1",
"Title": "Electrisch Gereedschap",
"ImagePath": "Assets/Group1.jpg",
"Items":
[
 {
    "ItemNo": "S010005",
    "Description": "Boormachine BOSCH GBM 13-123456",
    "UnitOfMeasure": "STUKS",
    "Delivery" : "2 Weken",
    "InPurchaseOrder" : "10",
    "InSalesOrder" : "49",
    "UnitPriceExclVAT": "90",
    "PurchasePrice": "60",
    "Margin": "30",
    "ActualStock": "100",
    "ItemBlocked" : "Nee",
    "ItemStatus": "Vrijgegeven",
    "ImagePath": "Assets/S010005_Large.jpg",
    "IsActivated": "true",
    "Characteristics":
    [{
        "Merk": "Bosch",
        "Kleur": "Blauw",
        "Prijsklasse": "1-100",
        "Toerental": "1500rpm",
        "Garantie": "1 Jaar"
    }]      
 },
 {
    "ItemNo": "S010006",
    "Description": "Boormachine BOSCH PSB 600RE",
    "UnitOfMeasure": "STUKS",
    "Delivery" : "2 Weken",
    "InPurchaseOrder" : "20",
    "InSalesOrder" : "6",
    "UnitPriceExclVAT": "75",
    "PurchasePrice": "50",
    "Margin": "25",
    "ActualStock": "31",
    "ItemBlocked" : "Nee",
    "ItemStatus": "Vrijgegeven",        
    "ImagePath": "Assets/S010006_Large.jpg",
    "IsActivated": "true",
    "Characteristics":
    [{
        "Fruit": "No",
        "Banana": "No",
        "Speed": "1-100",
        "Spinning": "yes",
        "Time": "1 year"
    }]
 }

1 个答案:

答案 0 :(得分:0)

好的,我做了一些有用的东西,如果有人遇到过这个问题:我就是这样做的。

foreach (JsonValue characteristicValue in itemObject["Characteristics"].GetArray()){
                    JsonObject characteristicObject = characteristicValue.GetObject();

                    var toObject = characteristicObject.GetObject();         
                    var lijst = toObject.ToList();

                    foreach (KeyValuePair<string, IJsonValue> characteristics in lijst)
                    {
                        string productCharacteristic = characteristics.Key.ToString();
                        ItemCharacteristic characteristic = new ItemCharacteristic(productCharacteristic, characteristicObject[productCharacteristic].GetString());

                        product.Characteristics.Add(characteristic);
                    }
                }

而不是

                foreach (JsonValue characteristicValue in itemObject["Characteristics"].GetArray()){
                JsonObject characteristicObject = characteristicValue.GetObject();
                ItemCharacteristic characteristic = new ItemCharacteristic(characteristicObject[""].GetString(),
                                                                                                characteristicObject[""].GetString())
            }