如何在C#中将Json转换为对象 [英] How to Convert Json to Object in C#

查看:1028
本文介绍了如何在C#中将Json转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在C#中将Json转换为Object.杰森在这里是:

I want Convert Json to Object in C#. Json here is:

[{"value":"e920ce0f-e3f5-4c6f-8e3d-d2fbc51990e4"}].

[{"value":"e920ce0f-e3f5-4c6f-8e3d-d2fbc51990e4"}].

如何使用Object进行操作.

How to do it using Object.

问题似乎很愚蠢,但并不是那么愚蠢. 我没有简单的Json,我有IEnumerable,并且我从usint JsonResult获得了json,如下所示:

Question seems silly but it is not so stupid. I have not simple Json, I have IEnumerable and I am getting json from usint JsonResult like this:

new JsonResult(来自User.Claims中的c,其中(c.Type.Equals("value"))选择new {c.Value});

new JsonResult(from c in User.Claims where (c.Type.Equals("value")) select new { c.Value });

此linq代码不适用于JObject.

This linq code does not work on JObject.

感谢Tommaso Belluzzo.

Thanks to Tommaso Belluzzo.

推荐答案

使用NewtonSoft Json.NET库( https://www.newtonsoft.com/json ),您可以执行以下操作:

Using NewtonSoft Json.NET library (https://www.newtonsoft.com/json), you can do as follows:

JObject result = JObject.Parse(jsonString);

但是您的Json字符串看起来更像一个数组,因此可能需要使用JArray.Parse来代替.此处带有示例的文档:

But your Json string looks more like an array, so probably JArray.Parse is what you need to use instead. Documentation with examples here:

https://www.newtonsoft.com/json/help/html/ParseJsonArray.htm

如果您想将内部元素解析为对象,那么这个问题的公认答案应该为您提供足够的提示:

If you want to parse the internal elements as objects, thhe accepted answer of this question should provide you enough hints:

C#解析对象的JSON数组

这篇关于如何在C#中将Json转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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