• C#中怎么解析JSON数据,并获取到其中的值?


    【1】首先我们根据创建一个json字符转

    string json = @"[{'phantom':true,'id':'20130717001','data':{'MID':1019,'Name':'aaccccc','Des':'cc','Disable':'启用','Remark':'cccc'}}]";

    【2】首先我们根据创建一个json字符转

    我们根据字符串的数据结构定义两个类:

      public class Info
            {
                public string phantom { get; set; }
                public string id { get; set; }
                public data data { get; set; }
            }

      public class data
            {
                public int MID { get; set; }
                public string Name { get; set; }
                public string Des { get; set; }
                public string Disable { get; set; }
                public string Remark { get; set; }
            }

    【3】再Main函数中进行操作,如下Main函数:

      static void Main(string[] args)
            {
                string json = @"[{'phantom':true,'id':'20130717001','data':{'MID':1019,'Name':'aaccccc','Des':'cc','Disable':'启用','Remark':'cccc'}}]";
                List<Info> jobInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Info>>(json);

                foreach (Info jobInfo in jobInfoList)
                {
                    Console.WriteLine("UserName:" + jobInfo.id);
                    Console.WriteLine("UserName:" + jobInfo.data.MID);
                }
            }

    运行结果如下:

    UserName:20130717001
    UserName:1019

    附加,如相同结构带数组,则使用递归遍历;

    【4】全部完整代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace JsonProcess
    {
        class Program
        {
            public class Info
            {
                public string phantom { get; set; }
                public string id { get; set; }
                public data data { get; set; }
            }

            public class data
            {
                public int MID { get; set; }
                public string Name { get; set; }
                public string Des { get; set; }
                public string Disable { get; set; }
                public string Remark { get; set; }
            }
            static void Main(string[] args)
            {
                string json = @"[{'phantom':true,'id':'20130717001','data':{'MID':1019,'Name':'aaccccc','Des':'cc','Disable':'启用','Remark':'cccc'}}]";
                List<Info> jobInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Info>>(json);

                foreach (Info jobInfo in jobInfoList)
                {
                    Console.WriteLine("UserName:" + jobInfo.id);
                    Console.WriteLine("UserName:" + jobInfo.data.MID);
                }
            }
        }
    }

    本文源于zhuxiaoge(http://www.cnblogs.com/zhuxiaoge/p/7095960.html),如有转载请标明出处,不甚感激!!!

  • 相关阅读:
    linux系统——机制与策略(三)
    linux系统——机制与策略(二)
    Linux系统——机制策略(一)
    RTSP会话基本流程
    linux编程学习
    编码风格——linux内核开发的coding style
    编程风格——整洁代码的4个提示
    编程风格——五种应该避免的代码注释
    十条不错的编程观点
    代码优化概要
  • 原文地址:https://www.cnblogs.com/zhuxiaoge/p/7095960.html
Copyright © 2020-2023  润新知