Json 反序列化器或正则表达式或 Json 解析以在 c# 中转换 Json 字符串
Json deserializer OR Regex Or Json parsing to convert a Json string in c#
我有以下 Json 文件,我想将其转换为不同的格式。新格式如下所示。
我们可以使用 Json 解析吗?或者我们可以使用正则表达式来更改修改 json 字符串。
我认为我们还可以使用自定义 json 反序列化器。谁能帮我用一些简单的方法来做到这一点?
当前格式
{
"PRETEST":
{"response":
{"a":{"source":"VeD","name":"a","value":null},
"b":{"source":"VeD","name":"b","value":"2XX"},
"c":{"source":"VeD","name":"c","value":"4933011630372431565180"},
"d":{"source":"VeD","name":"d","value":null},
"e":{"source":"VeD","name":"e","value":"EHD453REN00000004"},
"f":{"source":"VeD","name":"f","value":"HU55"},
"g":{"source":"VeD","name":"g","value":"453"},
"h":{"source":"VeD","name":"h","value":null},
"i":{"source":"VeD","name":"i","value":null}},
"httpcode":200},
"TEST":
{"response":
{"a":{"source":"VeD","name":"a","value":null},
"b":{"source":"VeD","name":"b","value":"3XX"},
"c":{"source":"VeD","name":"c","value":"5933011630372431565180"},
"d":{"source":"VeD","name":"d","value":null},
"e":{"source":"VeD","name":"e","value":"FHD453REN00000004"},
"f":{"source":"VeD","name":"f","value":"HU55"},
"g":{"source":"VeD","name":"g","value":"433"},
"h":{"source":"VeD","name":"h","value":null},
"i":{"source":"VeD","name":"i","value":null}},
"httpcode":200},
"INT":
{"response":
{"a":{"source":"VeD","name":"a","value":null},
"b":{"source":"VeD","name":"b","value":"4XX"},
"c":{"source":"VeD","name":"c","value":"1933011630372431565180"},
"d":{"source":"VeD","name":"d","value":null},
"e":{"source":"VeD","name":"e","value":"KHD453REN00000004"},
"f":{"source":"VeD","name":"f","value":"KU55"},
"g":{"source":"VeD","name":"g","value":"253"},
"h":{"source":"VeD","name":"h","value":null},
"i":{"source":"VeD","name":"i","value":null}},
"httpcode":200}}
新格式
{
"PRETEST":
{"response":
{"a"null,
"b":"2XX",
"c":"4933011630372431565180",
"d"::null,
"e":"EHD453REN00000004",
"f":"HU55",
"g":"453",
"h"::null,
"i"::null},
"httpcode":200},
"TEST":
{"response":
{"a":null,
"b""3XX"
"c":"5933011630372431565180",
"d":null,
"e""FHD453REN00000004",
"f""HU55",
"g""433",
"h":null,
"i":null},
"httpcode":200},
"INT":
{"response":
{"a":null,
"b""4XX",
"c":"1933011630372431565180",
"d":null,
"e":"KHD453REN00000004",
"f":"KU55",
"g":"253",
"h":null,
"i":null},
"httpcode":200},
"PREPROD":
{"response":
{"a":null,
"b":"5XX",
"c":"8933011630372431565180",
"d":null,
"e":"EHD453REN00000004",
"f":"HU55",
"g":"453",
"h":null,
"i":null},
"httpcode":200}}
是的。你可以。转到此网站 (http://jsonutils.com/) and make corresponding classes with the corresponding data annotations and finally, by using NewtonSoft (http://www.newtonsoft.com/json),呈现您的 json.
这是一个正则表达式解决方案。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\{[^{}]+value\":([^{}]+)\}";
string replacement = "";
Regex rgx = new Regex(pattern);
string input = YOUR_JSON_STRING;
string result = rgx.Replace(input, replacement);
Console.WriteLine(result);
}
}
我有以下 Json 文件,我想将其转换为不同的格式。新格式如下所示。 我们可以使用 Json 解析吗?或者我们可以使用正则表达式来更改修改 json 字符串。
我认为我们还可以使用自定义 json 反序列化器。谁能帮我用一些简单的方法来做到这一点?
当前格式
{
"PRETEST":
{"response":
{"a":{"source":"VeD","name":"a","value":null},
"b":{"source":"VeD","name":"b","value":"2XX"},
"c":{"source":"VeD","name":"c","value":"4933011630372431565180"},
"d":{"source":"VeD","name":"d","value":null},
"e":{"source":"VeD","name":"e","value":"EHD453REN00000004"},
"f":{"source":"VeD","name":"f","value":"HU55"},
"g":{"source":"VeD","name":"g","value":"453"},
"h":{"source":"VeD","name":"h","value":null},
"i":{"source":"VeD","name":"i","value":null}},
"httpcode":200},
"TEST":
{"response":
{"a":{"source":"VeD","name":"a","value":null},
"b":{"source":"VeD","name":"b","value":"3XX"},
"c":{"source":"VeD","name":"c","value":"5933011630372431565180"},
"d":{"source":"VeD","name":"d","value":null},
"e":{"source":"VeD","name":"e","value":"FHD453REN00000004"},
"f":{"source":"VeD","name":"f","value":"HU55"},
"g":{"source":"VeD","name":"g","value":"433"},
"h":{"source":"VeD","name":"h","value":null},
"i":{"source":"VeD","name":"i","value":null}},
"httpcode":200},
"INT":
{"response":
{"a":{"source":"VeD","name":"a","value":null},
"b":{"source":"VeD","name":"b","value":"4XX"},
"c":{"source":"VeD","name":"c","value":"1933011630372431565180"},
"d":{"source":"VeD","name":"d","value":null},
"e":{"source":"VeD","name":"e","value":"KHD453REN00000004"},
"f":{"source":"VeD","name":"f","value":"KU55"},
"g":{"source":"VeD","name":"g","value":"253"},
"h":{"source":"VeD","name":"h","value":null},
"i":{"source":"VeD","name":"i","value":null}},
"httpcode":200}}
新格式
{
"PRETEST":
{"response":
{"a"null,
"b":"2XX",
"c":"4933011630372431565180",
"d"::null,
"e":"EHD453REN00000004",
"f":"HU55",
"g":"453",
"h"::null,
"i"::null},
"httpcode":200},
"TEST":
{"response":
{"a":null,
"b""3XX"
"c":"5933011630372431565180",
"d":null,
"e""FHD453REN00000004",
"f""HU55",
"g""433",
"h":null,
"i":null},
"httpcode":200},
"INT":
{"response":
{"a":null,
"b""4XX",
"c":"1933011630372431565180",
"d":null,
"e":"KHD453REN00000004",
"f":"KU55",
"g":"253",
"h":null,
"i":null},
"httpcode":200},
"PREPROD":
{"response":
{"a":null,
"b":"5XX",
"c":"8933011630372431565180",
"d":null,
"e":"EHD453REN00000004",
"f":"HU55",
"g":"453",
"h":null,
"i":null},
"httpcode":200}}
是的。你可以。转到此网站 (http://jsonutils.com/) and make corresponding classes with the corresponding data annotations and finally, by using NewtonSoft (http://www.newtonsoft.com/json),呈现您的 json.
这是一个正则表达式解决方案。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\{[^{}]+value\":([^{}]+)\}";
string replacement = "";
Regex rgx = new Regex(pattern);
string input = YOUR_JSON_STRING;
string result = rgx.Replace(input, replacement);
Console.WriteLine(result);
}
}