C#怎么解析Json并提取所有内容到字典或List,要具体代码。

2024-11-24 14:40:37
推荐回答(5个)
回答1:

//需要引用附件dll
TextReader reader = File.OpenText("json.txt");
JsonReader readerJson = new JsonTextReader(reader);
Dictionary dict = new Dictionary();
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);
    }
}

效果图:


回答2:

五月花学校,软件开发培训,十六年办学经验

回答3:

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()
{
}

///


/// json序列化(非二进制方式)
///

///
///
///
public static string JsonSerializer(T t)
{
JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
return jsonSerialize.Serialize(t);

}

///
/// json反序列化(非二进制方式)
///

///
///
///
public static T JsonDeserialize(string jsonString)
{
JavaScriptSerializer jsonSerialize = new JavaScriptSerializer();
return (T)jsonSerialize.Deserialize(jsonString);
}

}
}

回答4:

告你个办法把,建好这个模型的对象,然后直接用jsonSerialize.Deserialize反解析到类型中就行了

回答5:

Newtonsoft.Json