如何将Json数组字符串转化为Json的List
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将Json数组字符串转化为Json的List相关的知识,希望对你有一定的参考价值。
参考技术A 将json字符串转成Java的Array数组private String json = "\"address\":\"chian\",\"birthday\":\"birthday\":\"2010-11-22\","+
"\"email\":\"email@123.com\",\"id\":22,\"name\":\"tom\"";
@Test
public void readJSON2Array()
try
json = "[" + json + "]";
jsonArray = JSONArray.fromObject(json);
Object[] os = jsonArray.toArray();
System.out.println(os.length);
Student[] stus = (Student[]) JSONArray.toArray(jsonArray, Student.class);
System.out.println(stus.length);
System.out.println(stus[0]);
catch (Exception e)
e.printStackTrace();
运行的结果如下:
==============JSON Arry String >>> Java Array ==================
#%%%"address":"chian","birthday":"birthday":"2010-11-22","email":"email@123.com","id":22,"name":"tom"
1
"address":"chian","birthday":"birthday":"2010-11-22","email":"email@123.com","id":22,"name":"tom"
"address":"chian","birthday":"birthday":"2010-11-22","email":"email@123.com","id":22,"name":"tom"
1
将JSON字符串转成Java的List集合
private String json = "\"address\":\"chian\",\"birthday\":\"birthday\":\"2010-11-22\","+
"\"email\":\"email@123.com\",\"id\":22,\"name\":\"tom\"";
public void readJSON2List()
try
json = "[" + json + "]";
jsonArray = JSONArray.fromObject(json);
List<Student> list = JSONArray.toList(jsonArray, Student.class);
System.out.println(list.size());
System.out.println(list.get(0));
list = JSONArray.toList(jsonArray);
System.out.println(list.size());
System.out.println(list.get(0));
catch (Exception e)
e.printStackTrace();
。。。本回答被提问者和网友采纳 参考技术B JSONArray.fromObject(array).toList();
在ASP.NET里list怎么转化为json
ASP.NET里面怎么把从数据库查到的list<class> 转化为json,只要JS弹出来的是JSON字符串就行了,请高手们帮忙
在ASP.NET里list转化为json的方法是传递到页面上用js方法转换。Handler.ashx中代码如下:
using System.Web.Script.Serialization;
JavaScriptSerializer jss = new JavaScriptSerializer();
//list数据
List<T> list = new List<T>();
Response.Write(jss.Serialize(list));
前台js:
$.ajax(
type: "POST",
url: "xxx/js",
data: 传递的数据 ,
success: function (result)
var data = eval("("+result+")");
//这里,你需要用循环,动态生成table,tr,td来显示result中的数据
,
complete: function (xhr, ts)
xhr = null;
,
error: function ()
alert(\'error\');
); 参考技术A 给class加上[Serializable]属性
然后可以通过JavaScriptSerializer类将List序列化成json字符串,这个类在System.Web.Script.Serialization命名空间下面,需要添加System.Web.Extensions的引用
我的.net版本是4.0追问
我是新手,麻烦写个列子,谢谢,我的也是4.0,在ASPX后台任意的方法里面写一个
追答//假设class结构如下
[Serializable]
public class MyClass
public string Nameget;set;
public string Codeget;set;
List List = new List(); //添加元素部分省略 JavaScriptSerializer jsSer = new JavaScriptSerializer(); string jsonStr = jsSer.Serialize(obj);
这里的jsonStr就是标准格式的json字符串了
要在页面上弹出,方法有很多种,这里用其中一种好了 ClientScript.RegisterStartupScript(this.GetType(), "json", "alert('" + jsonStr +"')", true);
try
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(data.GetType());
using (MemoryStream ms = new MemoryStream())
serializer.WriteObject(ms, data);
return Encoding.UTF8.GetString(ms.ToArray());
catch
return null;
追问
json提示不存在,我不是用的MVC4,在ASPX后台方法写的
工具引用中再添加 下面两个再试试
System.ServiceModel
System.ServiceModel.Web
List
UserList=new
List
();
System.Web.Script.Serialization.JavaScriptSerializer
oSerializer
=
new
System.Web.Script.Serialization.JavaScriptSerializer();
string
sJSON
=
oSerializer.Serialize(new
total=10,
rows
=
showList
);
Response.Write(sJSON);
Response.End();
好像是在。net4.0中开始有这个东东的,如果想在2.0中用那还是自个儿拼字符串吧。
以上是关于如何将Json数组字符串转化为Json的List的主要内容,如果未能解决你的问题,请参考以下文章