图片 10

JArray的基本操作

引用:

直入主题,不废话…

using Newtonsoft.Json.Linq;

1、JObject:基本的json对象

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

图片 1

1、JObject:基本的json对象

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

图片 2

image

如何获取json里的某个属性(节点)值,对其删改,新增

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

图片 3

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

图片 4

image

//2.1 数组用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;
    }

图片 5

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

图片 6

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

图片 7

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

图片 8

image

string jsonText = ``"[{'a':'aaa','b':'bbb','c':'ccc'},{'a':'aa','b':'bb,'c':'cc'}]"``;

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

图片 9

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

图片 10

image

 

总结:写接口的时候,类似上面的5种情况经常会遇到,有时候会弄糊涂,算一次简单的复习吧。

var mJObj = JArray.Parse(jsonText);

 

//需求,删除列表里的a节点的值为'aa'的项

 

IList<JToken> delList = ``new List<JToken>(); ``//存储需要删除的项

 

foreach (``var ss ``in mJObj )  ``//查找某个字段与值

{

if``(((JObject) ss)[``"a"``]==``'aa'``)

 

delList .add(ss);

}

 

foreach (``var item ``in delList )  ``//移除mJObj  在delList 里的项

{

 

mJObj .remove(item);

}

//2.2 非数组用JObject加载 (这里主要以这个为例子)

 

string jsonText = ``"[{'a':'aaa','b':'bbb','c':'ccc'}]"``;

 

var mJObj = JObject.Parse(jsonText t);

 

mJObj.Add() ``//新增,没试过

 

var v1=mJObj[a].ToString()  ``//得到'aaa'的值

以上的例子可交互灵活使用

 

注意事项,JArray 只能用来查找索引的项的对象,找到对象需要转换为
JObject,再查找对应的属性值

 

其他写法补充

1、属性下嵌套json (非数组)遍历其包含的属性列表

var xx = ((mJObj[“dim”])).Children().Values(); 

foreach (var item in xx)
{
var dimID = ((JObject) item)[“id”];

}

转载: