当前位置:网站首页>Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item

Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item

2022-04-23 20:24:00 First code

generally speaking , Although the data format returned to us by the data interface is json Format , But different formats use different parsing methods , It is mainly divided into array type and non array type , The difference between the two formats is as follows :

Array type :( There is an array of packages on the outside )

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

Use JArray The way

string jsonText = "[{'a':'aaa','b':'bbb','c':'ccc'},{'a':'aa','b':'bb,'c':'cc'}]";  
 
var mJObj = JArray.Parse(jsonText t);
 
// demand , Delete... From the list a The value of the node is 'aa' The item 
IList<JToken> delList = new List<JToken>(); // Store items that need to be deleted 
 
foreach (var ss in mJObj )  // Find a field and value 
{
   if(((JObject) ss)["a"]=='aa')
    delList .add(ss);
}
 
foreach (var item in delList )  // remove mJObj   stay delList  Items in 
{ 
    mJObj .remove(item); 
}

Non array type :( No array wrapping in the outer layer )

{'a':'aaa','b':'bbb','c':'ccc'}

Use JObecj The way

//2.2  Non array use JObject load  ( Here we mainly take this as an example )
string jsonText = "{'a':'aaa','b':'bbb','c':'ccc'}";  
 
var mJObj = JObject.Parse(jsonText t);
 
mJObj.Add() // newly added , Never tried. 
 
var v1=mJObj[a].ToString()  // obtain 'aaa' Value 

版权声明
本文为[First code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210551267632.html