C#中,处理JSON文件(解析与生成)

建议用控制台编译方式。
首先,添加引用:System.Web.Extensions
添加Newtonsoft.Json库;
并且需要应用命名空间:

using System.Web.Script.Serialization;
using Newtonsoft.Json;

然后创建所需的实体对象。以Person实体为例:

//先创建一个对象
  class Person
    {
        public string name { get; set; }
        public int age { get; set; }
        public override string ToString()
        {
            return string.Format("Name:{0}\nAge:{1}", name, age);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            解析JSON文件
            string JSONstring = File.ReadAllText("JSON.json");//这里用到IO,所以需要多加命名空间:using System.IO;
            JavaScriptSerializer ser = new JavaScriptSerializer();//用到命名空间:using System.Web.Script.Serialization;
            Person p1 = ser.Deserialize<Person>(JSONstring);//反序列化处理
            Console.WriteLine(p1);

            //生成JSON文件
            Person P2 = new Person() { name = "Ben", age = 58 };
            string outputJSON = ser.Serialize(P2);//序列化处理
            File.WriteAllText("output.json",outputJSON);

        }
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
使用C# Newtonsoft Json通过json数据,解码json生成用于保存数据类json类,通过json生成类代码,支持子类 自动 如 [{"itemsize":437,"pageId":1,"erpNickShopObj":null,"itemjbxx":[{"num":94628,"numIid":40239293981,"outerId":"异常1元连接","picUrl":"http://img04.taobaocdn.com/bao/uploaded/i4/TB1rdpFGXXXXXc5XFXXXXXXXXXX_!!1-item_pic.gif","price":"1.00","title":"[Enni.kr]<恩妮韩品>补邮费/订单异常关闭专拍连接","volume":0},{"num":65592,"numIid":36048181106,"outerId":"爱茉莉沐浴露套装200ml*3","picUrl":"http://img03.taobaocdn.com/bao/uploaded/i3/516137599/TB21kzXXVXXXXcUXXXXXXXXXXXX_!!516137599.jpg","price":"52.00","title":"韩国进口爱茉莉happybath 沐浴露 柠檬 玫瑰 薰衣草 200ml*3套装","volume":0},{"num":59765,"numIid":38272119734,"outerId":"7难题牙膏 绿款","picUrl":"http://img01.taobaocdn.com/bao/uploaded/i1/516137599/TB2xDbXXVXXXXbbXXXXXXXXXXXX_!!516137599.jpg","price":"9.90","title":"韩国进口LG贝瑞奥/倍瑞奥7难题解决牙膏7功能合一强健120G绿色","volume":0},{"num":59764,"numIid":38288409657,"outerId":"黄金皂2件套礼盒","picUrl":"http://img04.taobaocdn.com/bao/uploaded/i4/516137599/TB2XBbXXVXXXXbKXXXXXXXXXXXX_!!516137599.jpg","price":"90.00","title":"包邮 韩国纯金皂 24K金美容皂 天然特效美容黄金皂 2件套装 礼盒","volume":0},{"num":59763,"numIid":38272395230,"outerId":"7难题牙膏 蓝款","picUrl":"http://img02.taobaocdn.com/bao/uploaded/i2/516137599/TB204vXXVXXXXa5XpXXXXXXXXXX_!!516137599.jpg","price":"9.90","title":"韩国LG 贝瑞奥/倍瑞奥7难题解决牙膏 7功能合一 原味 120G 蓝色","volume":0},{"num":59758,"numIid":38278150798,"outerId":"9928牙膏绿色款","picUrl":"http://img03.taobaocdn.com/bao/uploaded/i3/516137599/TB2KDDXXVXXXXaEXXXXXXXXXXXX_!!516137599.jpg","price":"9.90","title":"韩国 LG贝瑞奥/倍瑞奥9928牙膏 强齿美白牙膏 120G 绿色","volume":0},{"num":59751,"numIid":38288605749,"outerId":"9928牙膏蓝色款","picUrl":"http://img04.taobaocdn.com/bao/uploaded/i4/516137599/TB2n9HXXVXXXXbxXXXXXXXXXXXX_!!516137599.jpg","price":"9.90","title":"韩国 LG 倍瑞奥 9928 牙膏 抗菌 牙龈护理 防蛀牙固定牙齿 120g","volume":0},{"num":59741,"numIid":38301128090,"outerId":"9928牙膏红色款","picUrl":"http://img01.taobaocdn.com/bao/uploaded/i1/516137599/TB2PATXXVXXXXcHXXXXXXXXXXXX_!!516137599.jpg","price":"9.90","title":"韩国 LG贝瑞奥/倍瑞奥9928牙膏 消炎
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏末初涼い

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值