Json反序列化器或正则表达式或Json解析以在c#中转换Json字符串

Posted

技术标签:

【中文标题】Json反序列化器或正则表达式或Json解析以在c#中转换Json字符串【英文标题】:Json deserializer OR Regex Or Json parsing to convert a Json string in c# 【发布时间】:2017-07-13 14:05:19 【问题描述】:

我有以下 Json 文件,我想将其转换为不同的格式。新格式如下所示。 我们可以为此使用 Json Parsing 吗?或者我们可以使用正则表达式来更改修改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

【问题讨论】:

【参考方案1】:

这是一个正则表达式的解决方案。

using System;
using System.Text.RegularExpressions;

public class Example

   public static void Main()
   
      string pattern = @"\[^]+value\":([^]+)\";
      string replacement = "$1";
      Regex rgx = new Regex(pattern);
      string input = YOUR_JSON_STRING;
      string result = rgx.Replace(input, replacement);
      Console.WriteLine(result);
   

【讨论】:

字符串模式 = "\\[^]+value\":([^]+)\\"; .. 工作正常。【参考方案2】:

是的。你可以。访问这个网站(http://jsonutils.com/)并使用相应的数据注释制作相应的类,最后使用NewtonSoft(http://www.newtonsoft.com/json)渲染您的json。

【讨论】:

我需要以编程方式执行此操作..您可以为此提供示例代码吗?

以上是关于Json反序列化器或正则表达式或Json解析以在c#中转换Json字符串的主要内容,如果未能解决你的问题,请参考以下文章

使用休眠 jpa 进行 JSON 序列化和反序列化以在 JSON 响应中将父对象转换为子对象

CSS 到 JSON 解析器或转换器

Newtonsoft.Json反序列化(Deserialize)报错问题

JSON的序列化和反序列化eval()和parse()方法以及stringfy()方法

在不反序列化的情况下操作 JSON

将 JSON 反序列化为 C# 对象以在网格中将嵌套数组显示为字符串