欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JObject,JArray的基本操作

程序员文章站 2022-04-01 19:21:00
引用:https://www.cnblogs.com/dacongge/p/6957074.html 1、JObject:基本的json对象 2、JObject:嵌套子对象(JObject嵌JObject) 3、JArray:基本json对象中的数组 4、JArray: 多个json对象数组 5、J ......

引用:https://www.cnblogs.com/dacongge/p/6957074.html

1、jobject:基本的json对象

    /// <summary>
    /// gets the j object.
    /// </summary>
    /// <returns></returns>
    public jobject getjobject()
    {
        var obj = new jobject {{"name", "mark" } };
        return obj;
    }

JObject,JArray的基本操作

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;
    }

JObject,JArray的基本操作

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;
    }

JObject,JArray的基本操作

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;
    }

JObject,JArray的基本操作

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;
    }

JObject,JArray的基本操作