C#怎么解析Json并提取所有内容到字典或List,要具体代码。
{"errorCode":22000,"data":{"xcode":"06b9ed05b37158341d07286be822d843","songList":[{"q...
{"errorCode":22000,"data":{"xcode":"06b9ed05b37158341d07286be822d843","songList":[{"queryId":"11277008","songId":11277008,"songName":"\u52c7\u6c14","artistId":"1095","artistName":"\u6881\u9759\u8339","albumId":11276429,"albumName":"\u5979\u0026\u5979 [\u597d\u5973\u771f\u7231\u6b4c]","songPicSmall":"http:\/\/b.hiphotos.baidu.com\/ting\/pic\/item\/18d8bc3eb13533fa50437d0daad3fd1f41345b18.jpg","songPicBig":"http:\/\/a.hiphotos.baidu.com\/ting\/pic\/item\/9d82d158ccbf6c814c0bc5cbbe3eb13533fa40d1.jpg","songPicRadio":"http:\/\/c.hiphotos.baidu.com\/ting\/pic\/item\/2cf5e0fe9925bc31e1a8efbb5cdf8db1cb137018.jpg","lrcLink":"\/data2\/lrc\/11301207\/11301207.lrc","version":"","copyType":3,"time":238,"linkCode":22000,"songLink":"http:\/\/file.qianqian.com\/\/data2\/music\/42016392\/42016392.mp3?xcode=06b9ed05b3715834080e0c237f8c5ca50a27df109cddc66b\u0026src=\u0022http%3A%2F%2Fpan.baidu.com%2Fshare%2Flink%3Fshareid%3D459557%26uk%3D2503337755\u0022","showLink":"http:\/\/pan.baidu.com\/share\/link?shareid=459557\u0026uk=2503337755","format":"mp3","rate":128,"size":3824863,"relateStatus":"0","resourceType":"2"}]}}
====解析后图片:
这是WinForm项目,源中的\u52c7\u6c14等要转换为中文。 展开
====解析后图片:
这是WinForm项目,源中的\u52c7\u6c14等要转换为中文。 展开
5个回答
展开全部
//需要引用附件dll
TextReader reader = File.OpenText("json.txt");
JsonReader readerJson = new JsonTextReader(reader);
Dictionary<object, object> dict = new Dictionary<object, object>();
object temp = new object();
while (readerJson.Read())
{
if (readerJson.Value != null)
{
switch (readerJson.TokenType)
{
case JsonToken.PropertyName:
dict.Add(readerJson.Value, new object());
temp = readerJson.Value;
break;
default:
dict[temp] = readerJson.Value;
break;
}
Console.WriteLine(readerJson.TokenType + "\t" + readerJson.Value);
}
}
效果图:
更多追问追答
追答
我用你给的那段没问题哈,是不是你的json和上面的不同呢?
http://pan.baidu.com/s/1o6FWBUE
展开全部
五月花学校,软件开发培训,十六年办学经验
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
namespace PublicUse.Text
{
public class JsonSerz
{
public JsonSerz()
{
}
/// <summary>
/// json序列化(非二进制方式)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string JsonSerializer<T>(T t)
{
JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
return jsonSerialize.Serialize(t);
}
/// <summary>
/// json反序列化(非二进制方式)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <returns></returns>
public static T JsonDeserialize<T>(string jsonString)
{
JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
return (T)jsonSerialize.Deserialize<T>(jsonString);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
namespace PublicUse.Text
{
public class JsonSerz
{
public JsonSerz()
{
}
/// <summary>
/// json序列化(非二进制方式)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string JsonSerializer<T>(T t)
{
JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
return jsonSerialize.Serialize(t);
}
/// <summary>
/// json反序列化(非二进制方式)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonString"></param>
/// <returns></returns>
public static T JsonDeserialize<T>(string jsonString)
{
JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
return (T)jsonSerialize.Deserialize<T>(jsonString);
}
}
}
追问
WinForm项目好像没有using System.Web.Script.Serialization;
而且我提供的源中含有有unicode中文字符,是不是要先转换?
真心求代码,劳烦了。
追答
添加引用System.Web;System.Web.Extensions;
中文的好像也可以用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
告你个办法把,建好这个模型的对象,然后直接用jsonSerialize.Deserialize反解析到类型中就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Newtonsoft.Json
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询