问题
I want to take all the key-value pairs after the \"tags\" under "instanceData" and make them key-value pairs under "properties".
I have this...
{
"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
}
}
I want this...
{
"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
}
}
Is there a simple way to convert this? I am attempting to do this with RegEx with no luck.
回答1:
I would recommend looking at something like this:
How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
Serialize List<KeyValuePair<string, string>> as JSON
Effectively you're going to need to strip out the one key you want to be parsed, and re-add that to your JSON object.
Json.NET - Newtonsoft's tool is great for working with JSON. http://www.newtonsoft.com/json
Easiest way to do it:
- Convert your entire JSON string to a Dictionary or to a
List<KeyValuePair<string,string>>.
- Take the instanceData bit you want to split apart, and then parse it into another C# object.
- Merge both objects together using some logic to ensure no duplicate keys.
- Serialize your object back into JSON
This is an easy way to do it, though not the most efficient way.
来源:https://stackoverflow.com/questions/42770923/how-do-i-convert-json-string-to-key-value-pairs