将 json 转换为 C# 数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9586585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert json to a C# array?
提问by Joey Morani
Does anyone know how to convert a string which contains json into a C# array. I have this which reads the text/json from a webBrowser and stores it into a string.
有谁知道如何将包含 json 的字符串转换为 C# 数组。我有这个从 webBrowser 读取 text/json 并将其存储到字符串中。
string docText = webBrowser1.Document.Body.InnerText;
Just need to somehow change that json string into an array. Been looking at Json.NET but I'm not sure if that's what I need, as I don't want to change an array into json; but the other way around. Thanks for the help!
只需要以某种方式将该 json 字符串更改为数组。一直在看 Json.NET,但我不确定这是否是我需要的,因为我不想将数组更改为 json;但反过来。谢谢您的帮助!
采纳答案by Icarus
just take the string and use the JavaScriptSerializer to deserialize it into a native object. For example, having this json:
只需获取字符串并使用 JavaScriptSerializer 将其反序列化为本机对象。例如,有这个 json:
string json = "[{Name:'John Simith',Age:35},{Name:'Pablo Perez',Age:34}]";
You'd need to create a C# class called, for example, Person defined as so:
您需要创建一个 C# 类,例如,定义为 Person 的类:
public class Person
{
public int Age {get;set;}
public string Name {get;set;}
}
You can now deserialize the JSON string into an array of Person by doing:
您现在可以通过执行以下操作将 JSON 字符串反序列化为 Person 数组:
JavaScriptSerializer js = new JavaScriptSerializer();
Person [] persons = js.Deserialize<Person[]>(json);
Here's a link to JavaScriptSerializer documentation.
Note: my code above was not tested but that's the ideaTested it. Unless you are doing something "exotic", you should be fine using the JavascriptSerializer.
注意:我上面的代码没有经过测试,但这就是测试它的想法。除非你正在做一些“异国情调”的事情,否则你应该可以使用 JavascriptSerializer。
回答by ken2k
Yes, Json.Net is what you need. You basically want to deserialize a Json string into an array of objects.
是的,Json.Net 正是您所需要的。您基本上想将 Json 字符串反序列化为objects.
See their examples:
看他们的例子:
string myJsonString = @"{
"Name": "Apple",
"Expiry": "\/Date(1230375600000+1300)\/",
"Price": 3.99,
"Sizes": [
"Small",
"Medium",
"Large"
]
}";
// Deserializes the string into a Product object
Product myProduct = JsonConvert.DeserializeObject<Product>(myJsonString);
回答by Jevgenij Kononov
using Newtonsoft.Json;
Install this class in package console This class works fine in all .NET Versions, for example in my project: I have DNX 4.5.1 and DNX CORE 5.0 and everything works.
在包控制台中安装这个类 这个类在所有 .NET 版本中都可以正常工作,例如在我的项目中:我有 DNX 4.5.1 和 DNX CORE 5.0,一切正常。
Firstly before JSON deserialization, you need to declare a class to read normally and store some data somewhere This is my class:
首先在JSON反序列化之前,你需要声明一个类来正常读取并在某处存储一些数据这是我的类:
public class ToDoItem
{
public string text { get; set; }
public string complete { get; set; }
public string delete { get; set; }
public string username { get; set; }
public string user_password { get; set; }
public string eventID { get; set; }
}
In HttpContent section where you requesting data by GET request for example:
在您通过 GET 请求请求数据的 HttpContent 部分中,例如:
HttpContent content = response.Content;
string mycontent = await content.ReadAsStringAsync();
//deserialization in items
ToDoItem[] items = JsonConvert.DeserializeObject<ToDoItem[]>(mycontent);
回答by Kevin Brydon
Old question but worth adding an answer if using .NET Core 3.0 or later. JSON serialization/deserialization is built into the framework (System.Text.Json), so you don't have to use third party libraries any more. Here's an example based off the top answer given by @Icarus
老问题,但如果使用 .NET Core 3.0 或更高版本,则值得添加答案。JSON 序列化/反序列化内置于框架 (System.Text.Json) 中,因此您不必再使用第三方库。这是一个基于@Icarus 给出的最佳答案的示例
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var json = "[{\"Name\":\"John Smith\", \"Age\":35}, {\"Name\":\"Pablo Perez\", \"Age\":34}]";
// use the built in Json deserializer to convert the string to a list of Person objects
var people = System.Text.Json.JsonSerializer.Deserialize<List<Person>>(json);
foreach (var person in people)
{
Console.WriteLine(person.Name + " is " + person.Age + " years old.");
}
}
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
}
}

