json数组转化为list,json字符串转list
这篇文章给大家聊聊关于json数组转化为list,以及json字符串转list对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。
怎样用C#把JSON数据转换为list或数组
//导入这个命名空间
using System.Web.Script.Serialization;
//这个命名空间需要引用几个,System.Web.XXX,具体我也记不了是哪几个,
//你看到System.Web.xxx的都引用。之后自己去掉没报错就是不需要的。
//导入命名空间完后,你可以这样声明对象
JavaScriptSerializer js= new JavaScriptSerializer();
//然后调用Json数据转对象的方法
//strJson是Json字符串数据(这里需要JSON是数组数据的前提下)
List<string> list= js.Deserialize<List<string>>(strJson);
//这里主要说一下js.Deserialize<T>(string);//T(泛型)是需要转换成的对象(数组,List,OBJECT)
//string是Json字符串数据
//这个方法返回的是T类型对象。(定义什么返回什么)
复杂json怎么使用fastjson一次性提取出来
JSON数据之使用Fastjson进行解析(一)
据说FastJson是目前最快的解析Json数据的库,而且是国人开发出来的开源库。顶一下,付上官方网址:h/code.alibabatech.com/wiki/pages/viewpage.action?pageId=2424946
要使用Fastjson,首先需要下载相对应的jar文件,在官网即可下载。
附上初学的第一个例子,多多指教:
复制代码
{
"statuses":[
{
"id": 912345678901,
"text":"How do I stream JSON in Java?",
"geo": null,
"user":{
"name":"json_newb",
"followers_count": 41
}
},
{
"id": 777777777888,
"text":"dfngsdnglnsldfnsl",
"geo": null,
"user":{
"name":"dsfgpd",
"followers_count": 24
}
}
]
}
复制代码
AllBean的Bean类:
复制代码
package com.lee.JsonToBean;
public class AllBean{
private long id;
private String text;
private String geo;
private UserBean userBean;
public long getId(){
return id;
}
public void setId(long id){
this.id= id;
}
public String getText(){
return text;
}
public void setText(String text){
this.text= text;
}
public String getGeo(){
return geo;
}
public void setGeo(String geo){
this.geo= geo;
}
public UserBean getUserBean(){
return userBean;
}
public void setUserBean(UserBean userBean){
this.userBean= userBean;
}
}
复制代码
UserBean的Bean类:
复制代码
package com.lee.JsonToBean;
public class UserBean{
private String name;
private int followers_count;
public String getName(){
return name;
}
public void setName(String name){
this.name= name;
}
public int getFollowers_count(){
return followers_count;
}
public void setFollowers_count(int followers_count){
this.followers_count= followers_count;
}
}
复制代码
解析类JsonBean:
复制代码
package com.lee.JsonToBean;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
*{
"statuses":[
{
"id": 912345678901,
"text":"How do I stream JSON in Java?",
"geo": null,
"user":{
"name":"json_newb",
"followers_count": 41
}
},
{
"id": 777777777888,
"text":"dfngsdnglnsldfnsl",
"geo": null,
"user":{
"name":"dsfgpd",
"followers_count": 24
}
}
]
}
**/
public class JsonBean{
RTFEditorKit rtf;
DefaultStyledDocument dsd;
String text;
public static void main(String[] args){
JsonBean bean= new JsonBean();
//把字符串转为Json对象,这是因为我的json数据首先是json对象
JSONObject jobj= JSON.parseObject(bean.readRtf(new File("json.rtf")));
//然后是jsonArray,可以根据我的json数据知道
JSONArray arr= jobj.getJSONArray("statuses");
//根据Bean类的到每一个json数组的项
List<AllBean> listBeans= JSON.parseArray(arr.toString(), AllBean.class);
//遍历
for(AllBean bean_: listBeans){
//我这个demo的json数据获得第一层的数据
System.out.println(bean_.getText());
System.out.println(bean_.getId());
//我这个demo的json数据获得第二层的数据
System.out.println(bean_.getUserBean().getFollowers_count());
}
}
//因为我把json数据放进rtf文件,这是读取rtf文件的json数据,转化为字符串
public String readRtf(File in){
rtf=new RTFEditorKit();
dsd=new DefaultStyledDocument();
try{
rtf.read(new FileInputStream(in), dsd, 0);
text= new String(dsd.getText(0, dsd.getLength()));
} catch(FileNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
} catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
} catch(BadLocationException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return text;
}
}
如果你还想了解更多这方面的信息,记得收藏关注本站。