Newtonsoft.Json.dll使用方法

前端之家收集整理的这篇文章主要介绍了Newtonsoft.Json.dll使用方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1:Newtonsoft.Json.dll 下载 http://json.codeplex.com/

2:解析JSON字符窜

方法1:

using Newtonsoft.Json;  
using System.Text;  
  
StringBuilder sb = new StringBuilder();  
                string str = "[{ID:'1',Name:'John',Other:[{Age:'21',Sex:'0'}]},{ID:'2',Name:'Good',Other:[{Age:'36',Sex:'1'}]}]";  
                JavaScriptArray javascript = (JavaScriptArray)JavaScriptConvert.DeserializeObject(str);  
  
                for (int i = 0; i < javascript.Count; i++ )  
                {  
                    JavaScriptObject obj = (JavaScriptObject)javascript[i];  
                    sb.Append("ID:" + obj["ID"].ToString());  
                    sb.Append("Name:" + obj["Name"].ToString());  
                    JavaScriptArray json = (JavaScriptArray)obj["Other"];  
  
string jsonText = "[{'a':'aaa','b':'bbb','c':'ccc'},{'a':'aaa2','b':'bbb2','c':'ccc2'}]";  
JsonReader reader = new JsonReader(new StringReader(jsonText));  
  
                while (reader.Read())  
                {  
                   textBox1.Text += "TokenType = " + reader.TokenType + "   ValueType =  " + reader.ValueType + "   Value = " + reader.Value + "\r\n";  
  
                }


方法2:不实用
 
                   for (int j = 0; j < json.Count; j++){  
                        JavaScriptObject jsonobj = (JavaScriptObject)json[j];  
                        sb.Append("Age:" + jsonobj["Age"].ToString());  
                        sb.Append("Sex:" + jsonobj["Sex"].ToString());  
                    }  
                } 
原文链接:https://www.f2er.com/json/290474.html

猜你在找的Json相关文章