2017-03-13 63 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

這是一個簡單的方法來做到這一點,雖然不是最有效的方式。