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

By admin in 4858.com on 2019年3月25日

引用:

直入大旨,不废话…

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

4858.com 1

1、JObject:基本的json对象

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

4858.com 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;
    }

4858.com 3

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

4858.com 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;
    }

4858.com 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;
    }

4858.com 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;
    }

4858.com 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;
    }

4858.com 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;
    }

4858.com 9

五 、JArray:json数组嵌套数组(2个学生对应多个学科分数)

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

4858.com 10

image

 

能够把下面的JSON看成贰个对象.你假如写对应的类即可

计算:写接口的时候,类似上边的5种情形日常会碰到,有时候会弄糊涂,算一次不难的复习吧。

var mJObj = JArray.Parse(jsonText);

JArray的基本操作,多层嵌套Json。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”:23,”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,\4858.com,”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地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有