如何将json字符串转换为键/值对? [英] How do I convert json string to key\value pairs?

查看:685
本文介绍了如何将json字符串转换为键/值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将" instanceData "下"\" 标签 \之后的所有键值对都设为""下的键值对属性".

I want to take all the key-value pairs after the \"tags\" under "instanceData" and make them key-value pairs under "properties".

我有这个...

{
  "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做到这一点,但是没有运气.

Is there a simple way to convert this? I am attempting to do this with RegEx with no luck.

推荐答案

我建议您查看以下内容:

I would recommend looking at something like this:

我如何才能将JSON反序列化为简单的Dictionary< string,string>在ASP.NET中?

序列化列表< KeyValuePair< string,string>>作为JSON

有效地,您将需要剥离要解析的一个密钥,然后将其重新添加到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的工具非常适合使用JSON. http://www.newtonsoft.com/json

Json.NET - Newtonsoft's tool is great for working with JSON. http://www.newtonsoft.com/json

最简单的方法:

  1. 将整个JSON字符串转换为字典或List<KeyValuePair<string,string>>.
  2. 获取要拆分的instanceData位,然后将其解析为另一个C#对象.
  3. 使用某种逻辑将两个对象合并在一起,以确保没有重复的键.
  4. 将对象序列化回JSON
  1. Convert your entire JSON string to a Dictionary or to a List<KeyValuePair<string,string>>.
  2. Take the instanceData bit you want to split apart, and then parse it into another C# object.
  3. Merge both objects together using some logic to ensure no duplicate keys.
  4. Serialize your object back into JSON

尽管不是最有效的方法,但这是一种简单的方法.

This is an easy way to do it, though not the most efficient way.

这篇关于如何将json字符串转换为键/值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆