多层嵌套Json,JArray的基本操作

引用:

直入核心,不废话…

using Newtonsoft.Json.Linq;

Newtonsoft.Json.Net20.dll
下载请访问

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里的某部属性(节点)值,对其删改,新增

在.net 2.0中领取那样的json

二、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加载

{“name”:”lily”,”age”:23,”addr”:{“city”:guangzhou,”province”:guangdong}}

三、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

三、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

 

引用命名空间

四、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

四、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'}]"``;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

五、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

多层嵌套Json,JArray的基本操作。5、JArray:json数组嵌套数组(1个学员对应八个学科分数)

    /// <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

 

能够把地点的JSON看成一个对象.你只要写对应的类即可

计算:写接口的时候,类似下面的四种情景平时会遭受,有时候会弄糊涂,算贰次简单的复习吧。

var mJObj = JArray.Parse(jsonText);

public class UserInfo

 

{

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

public string name;

 

public int age;

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

public address addr;

 

}

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

public class address

{

{

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

public string city;

 

public string province;
}

delList .add(ss);

下一场在条分缕析的地方如此写:

}

string
jsonData=”{\”name\”:\”lily\”,\”age\”:23,\”addr\”:{\”city\”:guangzhou,\”province\”:guangdong}}”;

 

UserInfo user=(UserInfo)JsonConvert.DeserializeObject(jsonData,
typeof(UserInfo));

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

取得City的值假若:user.addr.City;

{

如此达成也行

 

JObject jsonObj = JObject.Parse(jsonData);

mJObj .remove(item);

string name=jsonObj [“name”].ToString();

}

string age=jsonObj [“age”].ToString();

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

string city=((JObject )jsonObj [“addr”])[“city”].ToString();

 

string province=((JObject )jsonObj [“addr”])[“province”].ToString();

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

怎么着那么些json是动态的吧?譬如让你输入一个json,如{“name”:”lily”,”age”:二3,”addr”:
{“city”:guangzhou,”province”:guangdong}};
然后让您输入一个目的,如city,然后系统会输出guangzhou这几个值,那那样的话,json正是动态变化的了,笔者想询问有未有读取那样的json
的艺术。(注意,json是千家万户嵌套的。)

 

就用遍历

var mJObj = JObject.Parse(jsonText t);

public string GetJsonValue(JEnumerable<JToken> jToken,string
key)
{
IEnumerator enumerator = jToken.GetEnumerator();
while (enumerator.MoveNext())
{
JToken jc = (JToken)enumerator.Current;

 

if (jc is JObject||((JProperty)jc).Value is JObject)
{
return GetJsonValue(jc.Children(), key);
}
else
{
if (((JProperty)jc).Name == key)
{

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

return ((JProperty)jc).Value.ToString();
}
}
}
return null;
}

 

在调用的时候:

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

string jsonData =
“{\”name\”:\”lily\”,\”age\”:23,\”addr\”:{\”city\”:\”guangzhou\”,\”province\”:\”guangdong\”}}”;
JObject jsonObj = JObject.Parse(jsonData);
Response.Write(GetJsonValue(jsonObj.Children(), “province”));

以上的事例可相互灵活接纳

设若有多层嵌套的数组

 

string  jsonData =
“{\”addr\”:[{\”city\”:\”guangzhou\”,\”province\”:\”guangdong\”},{\”city\”:\”guiyang\”,\”province\”:\”guizhou\”}]}”;
JObject  jsonObj = JObject.Parse(jsonData);
JArray  jar = JArray.Parse(jsonObj[“addr”].ToString());
JObject  j = JObject.Parse(jar[0].ToString());
Response.Write(j[“city”]);

注意事项,JArray 只好用来查找索引的项的对象,找到对象要求更换为
JObject,再寻觅对应的属性值

JSON转XML

 

string
xmlstr=((XmlDocument)JsonConvert.DeserializeXmlNode(jsonData)).InnerXml.ToString();

别的写法补充

壹、属性下嵌套json (非数组)遍历其蕴藉的品质列表

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

foreach (var item in xx)
{
var dimID = ((JObject) item)[“id”]亚洲必赢官网 ,;

}

转载:

网站地图xml地图