string jsonStr = "";
之后就是处理这个jsonStr字符串了,
JObject jobject = JObject.Parse(jsonStr);//json格式转换
string results = jobject["results"].ToString();
这样就得到了json文件中你需要的属性results的值,
这里如果这个值results任然是一个json格式,而且是以数组的形式,需要再转换
"results": [{"electioncandidatestatus": null, "incumbent": true, "candidate_id": 176815, "district": "56", "party": "Republican", "photo": null, "office": "State House", "state_id": "ME", "match": 2.0, "name": "Richard Mason"}, {"electioncandidatestatus": null, "incumbent": null, "candidate_id": 176815, "district": "56", "party": "Republican", "photo": null, "office": "State House", "state_id": "ME", "match": 2.0, "name": "Richard Mason"}, {"electioncandidatestatus": null, "incumbent": true, "candidate_id": 128845, "district": null, "party": "", "photo": null, "office": "Senior Judge of the U.S. District Court", "state_id": "CO", "match": 1.3, "name": "Richard Mastch"}]
比如是以上的格式
JArray jarray = JArray.Parse(results);
再遍历jarray了,同上,
for (int i = 0; i < jarray.Count; i++)
string candidate_id = jarray[i]["candidate_id"].ToString();
string votesmartdistrict = jarray[i]["district"].ToString();
string votesmartoffice = jarray[i]["office"].ToString();
}
到这里就遍历完了。
首先你需要接收一个json的变量,string jsonStr = "";之后就是处理这个jsonStr字符串了,JObject jobject = JObject.Parse(jsonStr);//json格式转换string results = jobject["results"].ToString();这样就得到了json文件中你需要的属性results的值,这里如果这个值results任然...
可能我们用惯了 Newtonsoft.
Json
.dll 等第三方的类库来实现序列化等类似的操作,但是有时只是简单的用一下,感觉没必要那么费事,所谓杀鸡焉用牛刀,自制个小刀即可。
代码分享给大家,如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.Collections;
using System.Data.Common;
public class Convert
Json
#regio
System.Web.Extensions
c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Web.Extensions.dll
接下来就是两个函数了,一个是根据key来获取,一个是根据index来获取
public static bool GetValue(
string
json
,
string
key, out
string
value)
JSON
-----------JavaScript Object Notation
是一种轻量级的数据交换格式,因为采用完全独立于语言的文本格式【易于人阅读和编写】,所以一般使用
JSON
作为网络传输的格式,成为理想的数据交换“语言”。
它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集,使用了类似于C语言家族的习惯(包括C, C++,
C#
, Java, JavaScript, Perl, Python等)【易于机器解析和生
方法
一:Newtonsoft.
Json
.dll
1.首先项目需要引用Newtonsoft.
Json
.dll 程序集
第一步 先下载或者从别的项目
中
复制一个Newtonsoft.
Json
.dll 程序集,然后放到该项目下;
第二步 找到项目下“引用”,然后右键,点击“添加...
JSON
.parseObject 是将
Json
字符串
转
化为相应的对象;
JSON
.to
JSON
String
是将对象
转
化为
Json
字符串
两者主要用于前后台的数据传输过程
中
使用前需要先导入该包:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fast
json
</artifactId>
<version>1.2.62</version>
1. 引入
Json
.NET库,可以通过NuGet包管理器进行安装。
2. 使用
Json
Convert.DeserializeObject
方法
将
JSON
字符串
转
换为JArray对象。
3. 创建DataTable对象,并添加列。
4. 遍历JArray对象,将每个
JSON
对象
转
换为DataRow,并添加到DataTable
中
。
示例代码如下:
using Newtonsoft.
Json
;
using Newtonsoft.
Json
.Linq;
using System.Data;
string
json
= "{'data':[{'id':1,'name':'John'},{'id':2,'name':'Mary'}]}";
JObject obj = JObject.Parse(
json
);
JArray array = (JArray)obj["data"];
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(
string
));
foreach (JObject item in array)
DataRow row = dt.NewRow();
row["id"] = item["id"].Value<int>();
row["name"] = item["name"].Value<
string
>();
dt.Rows.Add(row);
以上代码将
JSON
字符串
中
的data数组
转
换为DataTable对象,包含id和name两列。