JSON转换为C#数组

JSON转换为C#数组

本文介绍了JSON转换为C#数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何包含JSON成C#数组字符串转换。我有这个从web浏览器,并将其存储到一个字符串读取文本/ JSON。

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.

string docText = webBrowser1.Document.Body.InnerText;

只是需要以某种方式改变JSON字符串数组。一直在寻找Json.NET,但我不知道这是我需要什么,因为我不希望改变阵列成JSON;但周围的其他方法。感谢您的帮助!

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!

推荐答案

只是采取串并使用的JavaScriptSerializer将其反序列化到一个本地对象。例如,有这个JSON:

just take the string and use the JavaScriptSerializer to deserialize it into a native object. For example, having this json:

string json = "[{Name:'John Simith',Age:35},{Name:'Pablo Perez',Age:34}]";

你需要创建一个名为C#类,例如,人定义为这样:

You'd need to create a C# class called, for example, Person defined as so:

public class Person
{
 public int Age {get;set;}
 public string Name {get;set;}
}

您现在可以通过做反序列化JSON字符串转换成Person的一个数组:

You can now deserialize the JSON string into an array of Person by doing:

JavaScriptSerializer js = new JavaScriptSerializer();
Person [] persons =  js.Deserialize<Person[]>(json);

下面是一个link到的JavaScriptSerializer文档。

进行了测试。除非你正在做的事情性趣,你应该罚款使用的JavaScriptSerializer。

Tested it. Unless you are doing something "exotic", you should be fine using the JavascriptSerializer.

这篇关于JSON转换为C#数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 18:21