2017-03-13 59 views
0

我要采取一切后,\“标签 \”的键值对下‘instanceData’,并‘性能’下他们的键值对。如何将json字符串转换为键值对?

我有这个...

{ 
    "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\":\"[email protected]\",\"productLine\":\"Information Components\",\"secondaryowner\":\"[email protected]\",\"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":"[email protected]", 
    "productLine":"Information Components", 
    "secondaryowner":"[email protected]", 
    "version":"1.0.0.0", 
    "meterId": "12345ab-259d-4206-a6ae-12345678abcd", 
    "infoFields": {}, 
    "quantity": 0.0004 
    } 
} 

是否有转换这种简单的方法是什么?我试图用RegEx做到这一点,但没有运气。

+1

与正则表达式?这听起来像一场噩梦。不要这样做。什么语言? –

+0

bash,php,java?必须有更多的正则表达式 –

+0

我正在使用C#编写库# – Gooch

回答

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

这是一个简单的方法来做到这一点,虽然不是最有效的方式。