简体   繁体   English

任何JSON字符串无需定义pojo即可转换为C#对象

[英]any JSON string convert into C# Object without defining pojo

I have to de-serialize any given JSON String to C# Object. 我必须将任何给定的JSON字符串反序列化为C#对象。 The JSON String is dynamic. JSON字符串是动态的。 So i cannot define Class structure for that Object. 所以我不能为该对象定义类结构。 I want some dynamic approach where C# should handle internally. 我想要一些动态的方法,其中C#应该在内部进行处理。

I have already tried using 我已经尝试使用

dynamic myObject = JsonConvert.DeserializeObject<dynamic>(Json);

Above gives me some meta data counts and complex structure which was not helpful. 上面给了我一些元数据计数和复杂的结构,这没有帮助。 I just need only object. 我只需要对象。 Also i have used 我也用过

object result = new JavaScriptSerializer().DeserializeObject(Json);

above does not handle the Array type. 上面的方法不处理数组类型。 sample JSON's 样本JSON

json1:[{"associatedToId":null,"associationType":null,"isObsolete":null}]

json2:{"commercialName":"[\"\",\"\"]","commercialActivity":"[\"\",\"\"]"}

json3:["شر*************************","Gu******************************"]

I need code to deserialize it to an object. 我需要将其反序列化为对象的代码。 which i will be using further for recursion and correcting data. 我将进一步使用它来递归和更正数据。

You can do this by deserialising to a dictionary object, which itself is a set or name/value pairs. 您可以通过反序列化字典对象来实现此目的,字典对象本身就是一个集合或名称/值对。

For an example, see here: 有关示例,请参见此处:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM