如何安全地反序列化键/数据格式JSON响应

时间:2018-04-28 02:16:34

标签: c# arrays json web-services json.net

我从第三方Web服务获得这个'优化'JSON响应如下所示,我根据键中提供的信息构建了类型,但我面临的困难是数据没有在对象中表示但在数组,有没有办法自动化我的对象列表的构建和值映射?

{
    "reports": {
        "query": {
            "keys": [{
                "name": "Date",
                "type": "date"
            }, {
                "name": "NetSales",
                "type": "price"
            }, {
                "name": "GrossCash",
                "type": "price"
            }, {
                "name": "GrossCard",
                "type": "price"
            }, {
                "name": "GrossDelivery",
                "type": "price"
            }, {
                "name": "NetFood",
                "type": "price"
            }, {
                "name": "NetDrink",
                "type": "price"
            }, {
                "name": "NetRetail",
                "type": "price"
            }, {
                "name": "Baskets",
                "type": "int"
            }],
            "data": [
                [1523577600000, 51924, 11300, 27200, 9900, null, null, null, 8],
                [1523404800000, 7434, 2600, 3900, null, null, null, null, 6],
                [1523491200000, 18101, 4000, 10100, null, null, null, null, 5],
                [1523664000000, 13243, 7400, 6500, null, null, null, null, 3],
                [1523750400000, 11718, 7300, null, 5000, null, null, null, 2],
                [1523836800000, 16576, 7700, 4800, 4900, null, null, null, 4],
                [1524096000000, 20293, 9100, 6000, null, null, null, null, 4]
            ]
        }
    },
    "api": {
        "message": {
            "success": {
                "202": {
                    "id": 202,
                    "title": "Token is valid",
                    "details": "Token is validated and found valid."
                }
            }
        },
        "codeBaseVersion": 1,
        "executionTime_milliSeconds": 43
    }
}

我最终得到了这段代码,但对此并不满意:

JObject myOpject = JObject.Parse(responseString);

List<JToken> setOfObjects = myOpject["reports"]["query"]["data"].Children().ToList();
var listOfData = new List<Data2>();
foreach (var token in setOfObjects)
{
    var myObject = new Data2
    {
        NetSales = decimal.Parse(token[1].ToString()),
        //etc.

    };

    listOfData.Add(myObject);
}

1 个答案:

答案 0 :(得分:3)

使用相同的JToken想法使用键转换数据以创建新的对象模型,然后将其反序列化为所需的强类型。

例如,记录一些记录可以将密钥和数据转换为

[
  {
    "Date": 1523577600000,
    "NetSales": 51924,
    ...
  },
  {
    "Date": 1523404800000,
    "NetSales": 7434,
    ...
  },
  ...
]

以下代码尝试使用与数据数组

中的item索引匹配的键索引来构建它
JObject myOpject = JObject.Parse(responseString);
List<JToken> keys = myOpject["reports"]["query"]["keys"].Children().ToList();
List<JToken> data = myOpject["reports"]["query"]["data"].Children().ToList();

var array = new JArray();
foreach (var token in data) {
    var record = new JObject();
    for (var i = 0; i < keys.Count; i++) {
        var propertyName = keys[i]["name"].Value<string>();
        var propertyValue = token[i];
        record[propertyName] = propertyValue;                    
    }
    array.Add(record);                
}

var listOfData = array.ToObject<List<Data2>>(); // or Data2[], etc

假设Data2是具有匹配属性的强类型模型。