C# 关于JArray和JObject封装JSON对象

直入主题,不废话...

1、JObject:基本的json对象

    /// <summary>
    /// Gets the j object.
    /// </summary>
    /// <returns></returns>
    public JObject GetJObject()
    {
        var obj = new JObject {{"Name", "Mark" } };
        return obj;
    }

image

2、JObject:嵌套子对象(JObject嵌JObject)

    /// <summary>
    /// Gets the j object.
    /// </summary>
    /// <returns></returns>
    public JObject GetJObject()
    {
        var obj = new JObject {{"Name", "Mark"}, {"Age", 8 }};
        var info = new JObject {{"Phone", "132****7777"}, {"Gender", "男"}};
        obj.Add("Info", info);
        return obj;
    }

image

3、JArray:基本json对象中的数组

    /// <summary>
    /// Gets the j array.
    /// </summary>
    /// <returns></returns>
    public JArray GetJArray()
    {
        var jarray = new JArray();
        var mark = new JObject { { "Name", "Mark" }, { "Age", 8 } };
        var jack = new JObject { { "Name", "Jack" }, { "Age", 9 } };
        jarray.Add(mark);
        jarray.Add(jack);
        return jarray;
    }

image

4、JArray: 多个json对象数组

    /// <summary>
    /// Gets the j array.
    /// </summary>
    /// <returns></returns>
    public JObject GetJArray()
    {
        var obj = new JObject();
        var student = new JArray
        {
            new JObject {{ "Name", "Mark" }, { "Age", 8 } },
            new JObject {{ "Name", "Jack" }, { "Age", 9 } }
        };
        var results = new JArray
        {
            new JObject {{ "Subject", "语文"}, { "Score", 100}},
            new JObject {{ "Subject", "数学" }, { "Score", 88}}
        };
        obj.Add("Student", student);
        obj.Add("Results", results);
        return obj;
    }

image

5、JArray:json数组嵌套数组(一个学生对应多个课程分数)

    /// <summary>
    /// Gets the results.
    /// </summary>
    /// <returns></returns>
    public JObject GetResults()
    {
        var mark = new JObject { { "Name", "Mark" }, { "Age", "8" } };
        var results = new JArray
        {
            new JObject {{ "Subject", "语文"}, { "Score", 100}},
            new JObject {{ "Subject", "数学" }, { "Score", 88}}
        };            
        mark.Add("Results", results);
        return mark;
    }

文章转载:https://www.jianshu.com/p/bd567de57308

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值