日期:2014-05-17  浏览次数:20430 次

Json格式字符串转对象

string s="
{
 "name":"userName",
 "Kind":1,
 "Gender":"f",
 "Info":{
            "Place":5,
            "comments":"UserCommnt"
 },
 "Score":5
}
";


我想把这个字符串中的name,Kind,Gender,Info(Place,comments),Score的值取出来。

我通过.NET的JSON对象取值,系统提出报错内容为:字符串格式不正确,代码如下



StreamReader sr= new StreamReader(instream, encoding);
JsonTextParser parser = new JsonTextParser();
JsonObjectCollection obj = parser.Parse(sr.ReadToEnd()) as JsonObjectCollection;
JsonUtility.GenerateIndentedJsonText = false; //其中的sr的值即为string s内容

string Name= obj["name"].GetValue().ToString();


请问,我应该如何操作string获取想要的内容?

JSON .NET 序列化

------解决方案--------------------
http://www.oksvn.com/Download/Detail-438.shtml
------解决方案--------------------
那你去下载Newtonsoft.Json.dll用吧,好用
------解决方案--------------------
试试 LitJSON.dll
------解决方案--------------------
string s="
{
 "name":"userName",
 "Kind":1,
 "Gender":"f",
 "Info":{
            "Place":5,
            "comments":"UserCommnt"
 },
 "Score":5
}
";

改成 第一个标红 改成 [   第二个标红 改成 ]