C#实现Json嵌套数组文件解析为对象
发布日期:2021-05-10 09:24:54 浏览次数:20 分类:精选文章

本文共 3401 字,大约阅读时间需要 11 分钟。

JSON���������������������������������

������������������

������������������������������������������JSON������������������������������������������������JSON������������������������������������������������������

  • ������������������������������������������������������������������List���������������������������������������������������������������������������
  • ���������������������:Newtonsoft.json.dll���������JSON������������������������:List���������������������������
  • ������������������

    2.1 ������������������������������������MapInfo.cs
    namespace JsonDemo
    {
    public class MapInfo
    {
    public string mapName { get; set; }
    public string count { get; set; }
    public string type { get; set; }
    public string url { get; set; }
    public string progress { get; set; }
    public string waitCount { get; set; }
    }
    }
    2.2 ������������������������������TestInfo.cs
    using System.Collections.Generic;
    namespace JsonDemo
    {
    public class TestInfo
    {
    public string XMText { get; set; }
    public string XMTYPE { get; set; }
    public string XMCount { get; set; }
    public List
    mapInfos
    {
    get; set;
    }
    }
    }
    2.3 ������JSON���������������������������
    using JsonDemo;
    using Kernal;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    namespace XmlHelper
    {
    public class Program
    {
    public static void Main(string[] args)
    {
    // ������JSON������������
    string filepath = $@"e:\Zhonghejiaotong.json";
    // ������JSON������
    List
    testInfos = new List
    ();
    var jsonData = GetJsonFile(filepath);
    // ���JSON������������������������
    var deserializeObject = JsonConvert.DeserializeObject947)
    List
    >(jsonData);
    // ������������������
    foreach (var item in testInfos)
    {
    // ���������������������
    Console.WriteLine($\"{item.XMText}\": {item.XMTYPE}\", {item.XMCount}");
    // ������������������������������
    var mapInfos = item.mapInfos;
    foreach (var mapInfo in mapInfos)
    {
    Console.WriteLine($"\t{mapInfo.mapName}\: {mapInfo.count}");
    Console.WriteLine($"\t\t{mapInfo.type}\: {mapInfo.url}");
    Console.WriteLine($"\t\t{mapInfo.progress}\: {mapInfo.waitCount}");
    Console.WriteLine();
    }
    }
    Console.ReadLine();
    }
    // ���������������������������
    public static string GetJsonFile(string filepath)
    {
    string json = string.Empty;
    using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
    using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
    {
    json = sr.ReadToEnd();
    }
    }
    return json;
    }
    }
    }

    ������

    ���������������������������������C#���������������JSON������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:Unity中实现VideoPalyer控件操作本地视频
    下一篇:C#实现对象为Json嵌套数组文件的创建

    发表评论

    最新留言

    能坚持,总会有不一样的收获!
    [***.219.124.196]2025年04月01日 12时02分29秒