C#封装仿Java式解析JSON

本文介绍如何在C#中使用Newtonsoft.Json库,封装仿Java方式来操作JSON,提供更符合Java开发者习惯的JSONObject和JSONArray类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近写了C#代码,然而C#操作json的方式实在是不习惯,于是就模仿Java解析json的写法做了一层封装。
引用库:Newtonsoft.Json

JSONObject:

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

namespace JSONPackaging {
    public class JSONObject {

        private JObject jObject;

        private JSONObject(JObject jObject) {
            this.jObject = jObject;
        }

        public JSONObject() {
            this.jObject = new JObject();
        }

        public static JSONObject StringParseJSON(string str) {
            JObject jo = JsonConvert.DeserializeObject(str) as JObject;
            JSONObject j = new JSONObject(jo);
            return j;
        }

        public string GetString(string key) {
            return (string)jObject[key];
        }

        public int GetInt(string key) {
            return (int)jObject[key];
        }

        public void Put(string key,string value) {
            CheckJobject();
            jObject[key] = value;

        }

        private void CheckJobject() {
            if(jObject == null) {
                jObject = new JObject();
            }
        }

        private string WipeOffCarriageReturn(string str) {
            return str.Replace("\r", "").Replace("\n", "").Replace(" ", "");
        }

        public void Put(string key,int value) {
            CheckJobject();
            jObject[key] = value;
        }

        public void Put(string key,JSONArray jsonArray) {
            jObject[key] = JArray.Parse(jsonArray.ToString());
        }

        public override string ToString() {
            string str = jObject.ToString();
            return WipeOffCarriageReturn(str).Trim();
        }

        public JSONArray GetJsonArray(string key) {
            return JSONArray.Parse(jObject[key].ToString());
        }

    }
}

JSONArray:

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

namespace JSONPackaging {
    public class JSONArray {

        private JArray jArray;
        public int Count { get; private set; }

        public JSONArray() {
            this.jArray = new JArray();
        }

        private JSONArray(JArray j) {
            this.jArray = j;
        }

        public static JSONArray Parse(string str) {
            JArray jrr = JArray.Parse(str);
            JSONArray j = new JSONArray(jrr);
            j.Count = jrr.Count;
            return j;
        }

        public JSONObject GetIndex(int i) {
            return JSONObject.StringParseJSON(jArray[i].ToString());
        }

        public void Add(JSONObject jsonObject) {
            this.jArray.Add((JObject)JsonConvert.DeserializeObject(jsonObject.ToString()));
            Count = jArray.Count;
        }

        public void Clear() {
            this.jArray.Clear();
        }

        private string WipeOffCarriageReturn(string str) {
            return str.Replace("\r", "").Replace("\n", "").Replace(" ", "");
        }

        public override string ToString() {
            string str = jArray.ToString();
            return WipeOffCarriageReturn(str).Trim();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值