如何将json字符串转换为键\值对?

时间:2017-03-13 18:31:07

标签: c# .net json azure-billing-api

我想在“ instanceData ”下的\“标记 \”之后获取所有键值对,并在“”下设置键值对特性”。

我有这个......

{
  "id": "/subscriptions/1234abcd-ab12-12ab-12ab-abcdfghi1234/Daily_BRSDT_20161214_0000",
  "name": "Daily_BRSDT_20161214_0000",
  "type": "Microsoft.Commerce/UsageAggregate",
  "properties": {
    "subscriptionId": "1234abcd-ab12-12ab-12ab-abcdfghi1234",
    "usageStartTime": "2017-03-08T00:00:00+00:00",
    "usageEndTime": "2017-03-09T00:00:00+00:00",
    "meterName": "Standard IO - File Read Operation Units (in 10,000s)",
    "meterCategory": "Data Management",
    "unit": "10,000s",
    "instanceData": "{\"Microsoft.Resources\":{\"resourceUri\":\"/subscriptions/1234abcd-ab12-12ab-12ab-abcdfghi1234/resourceGroups/default-resource-group67/providers/Microsoft.Storage/storageAccounts/defaultstorage67\",\"location\":\"ussouthcentral\",\"tags\":{\"ProjectName\":\"default Portal\",\"billTo\":\"Technology\",\"component\":\"Persistant Storage\",\"department\":\"Technology\",\"displayName\":\"default Portal Storage Account\",\"enviornment\":\"default\",\"function\":\"Reporting\",\"matterNumber\":\"999999\",\"primaryowner\":\"john@internet.com\",\"productLine\":\"Information Components\",\"secondaryowner\":\"mary@internet.com\",\"version\":\"1.0.0.0\"}}}",
    "meterId": "12345ab-259d-4206-a6ae-12345678abcd",
    "infoFields": {},
    "quantity": 0.0004
  }
}

我想要这个......

{
  "id": "/subscriptions/1234abcd-ab12-12ab-12ab-abcdfghi1234/Daily_BRSDT_20161214_0000",
  "name": "Daily_BRSDT_20161214_0000",
  "type": "Microsoft.Commerce/UsageAggregate",
  "properties": {
    "subscriptionId": "1234abcd-ab12-12ab-12ab-abcdfghi1234",
    "usageStartTime": "2017-03-08T00:00:00+00:00",
    "usageEndTime": "2017-03-09T00:00:00+00:00",
    "meterName": "Standard IO - File Read Operation Units (in 10,000s)",
    "meterCategory": "Data Management",
    "unit": "10,000s",
    "instanceData": "{\"Microsoft.Resources\":{\"resourceUri\":\"/subscriptions/1234abcd-ab12-12ab-12ab-abcdfghi1234/resourceGroups/default-resource-group67/providers/Microsoft.Storage/storageAccounts/defaultstorage67\",\"location\":\"ussouthcentral\"}}",
    "ProjectName":"default Portal",
    "billTo":"Technology",
    "component":"Persistant Storage",
    "department":"Technology",
    "displayName":"default Portal Storage Account",
    "enviornment":"default",
    "function":"Reporting",
    "matterNumber":"999999",
    "primaryowner":"john@internet.com",
    "productLine":"Information Components",
    "secondaryowner":"mary@internet.com",
    "version":"1.0.0.0",
    "meterId": "12345ab-259d-4206-a6ae-12345678abcd",
    "infoFields": {},
    "quantity": 0.0004
  }
}

有一种简单的方法来转换它吗?我试图用RegEx做这件事而没有运气。

1 个答案:

答案 0 :(得分:0)

我建议看看这样的事情:

How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?

Serialize List<KeyValuePair<string, string>> as JSON

实际上,您需要删除要解析的一个密钥,然后将其重新添加到JSON对象中。

Json.NET - Newtonsoft的工具非常适合使用JSON。 http://www.newtonsoft.com/json

最简单的方法:

  1. 将整个JSON字符串转换为字典或List<KeyValuePair<string,string>>.
  2. 将要拆分的instanceData位取出,然后将其解析为另一个C#对象。
  3. 使用一些逻辑将两个对象合并在一起,以确保没有重复的密钥。
  4. 将您的对象序列化为JSON
  5. 这是一种简单的方法,但不是最有效的方法。