1 .此方法的作用是将json格式的数据转换为数组格式。
2 .有Person类,json类型数据str=str={“name”:“hhdxwz”、“age”3360“1”}、{“name”3360“快泥猴桃”,}
listlistt=JSON.parseArray(str,Person.class ); listt现在可以接收str
首先构建两个类。
TestData类:
package com.xzw.test;
公共类测试数据{
私有字符串id; 私密int arrtibute; 隐私插入排序; 公共字符串getid () { return id; }公共语音设置(stringid ) { this.id=id; }public int getArrtibute () { return arrtibute; } publicvoidsetarrtibute (intarrtibute ) { this.arrtibute=arrtibute; }public int getSort () { return sort; }publicvoidsetsort(intsort ) { this.sort=sort; }
RecVo类:
package com.xzw.test;
import java.util.List;
公共类恢复{
私有列表数据恢复; 公共列表数据getr ecvo () { return RecVo; }公共语音服务(listtestdatarecvo ) { RecVo=recVo; }
调用函数代码:
RecVo recVo=new RecVo (; 列表数据列表=new ArrayList (; 测试数据测试数据1=new test数据(; testdata1.setarrtibute(28; testdata1.setsort(5; testdata1.setid(L000002 ); test数据test data2=new test data (; tstdata2.setarrtibute(28; testdata2.setsort(9; testdata2.setid(L000012 ); list.add(testdata1; list.add (测试数据2; recvo.setrecvo (列表; stringstr=JSON.tojsonstring (recvo ); system.out.println(str;//通过recvo.class将str解析为对象recvotoobj=JSON.parse object (str,recvo.class ); system.out.println (to obj.getr ecvo ().size ) ); stringarrjson=JSON.tojsonstring (列表); system.out.println(ArrJSON; 基于//TestData.class,将arrJson解析为数组listtestdataarrlist=JSON.parse array (arr JSON,testdata.class ); system.out.println (arrlist.get (0).getArrtibute ) ); 结果:
{“recvo”3360 [“arrtibute”:28、“id”:“l 000002”、“sort”:5 }、{“arrtibute”3:28、}
2
[{“arrtibute”:28、“id”:”l000002、“sort”:5}、{“arrtibute”:28、“id”3:”l 00000000012
28
此示例很好地说明了toJSONString、parseObject和parseArray的作用。