zoukankan      html  css  js  c++  java
  • C#应用Newtonsoft.Json操作json

    Newtonsoft.Json是一个开源的C#操作json的项目,应用起来非常简单。其github地址;

    下面的代码演示了如何应用Newtonsoft.Json序列号和反序列化。

    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    namespace JsonNetDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Project> pList = new List<Project>();
                Project p = new Project();
                p.TestSystemList = new List<TestSystem>();
                p.entrustCellphone = "12";
                p.entrustName = "12";
                p.entrustUnit = "12";
                p.goodsModel = "12";
                p.pid = 34199;
                p.productionCellphone = "12";
                TestSystem t = new TestSystem();
                t.eid = 78611;
                t.name = "5.8G无线局域网(移动终端)";
                p.TestSystemList.Add(t);
                pList.Add(p);
                var json = JsonConvert.SerializeObject(pList);
                Console.WriteLine(json);
                Console.Read();
                string s = "[{"productionCellphone":"12","entrustCellphone":"12","entrustUnit":"12","pid":34199,"goodsModel":"12","entrustName":"12","TestSystemList":[{"eid":78611,"name":"5.8G无线局域网(移动终端)"}]}]";
                var ret = JsonConvert.DeserializeObject<List<Project>>(s);
                Console.WriteLine(ret.Count);
                Console.Read();
            }
        }
        class Project
        {
            public string productionCellphone { get; set; }
            public string entrustCellphone { get; set; }
            public string entrustUnit { get; set; }
            public long pid { get; set; }
            public string goodsModel { get; set; }
            public string entrustName { get; set; }
            public List<TestSystem> TestSystemList { get; set; }
        }
        class TestSystem
        {
            public long eid { get; set; }
            public string name { get; set; }
        }
    }
    
  • 相关阅读:
    Elasticsearch 索引文档如何使用自动生成 Id?
    Spring Boot 缓存 知识点
    table的各种用法
    Spring Boot 整合 Elasticsearch
    Spring Boot 集成 Kafka
    Spring Boot 2实现分布式锁——这才是实现分布式锁的正确姿势!
    Spring Cloud 与 Spring Boot 版本兼容关系
    Spring Boot 之:Spring Boot Admin
    JVM 性能调优工具
    Spring Boot 之:Actuator 监控
  • 原文地址:https://www.cnblogs.com/wardensky/p/4528352.html
Copyright © 2011-2022 走看看