JSONArray数据转换成java List

Stella981
• 阅读 735

1.后台接收json数组转成封装实体类的List:

JSONArray数据转换成java List
package no.integrasco.ingentia.news.qaedition;

public class Person {

</span><span style="color: #0000ff">private</span><span style="color: #000000"> String name;

</span><span style="color: #0000ff">private</span> <span style="color: #0000ff">int</span><span style="color: #000000"> age;

</span><span style="color: #0000ff">public</span><span style="color: #000000"> String getName() {

    </span><span style="color: #0000ff">return</span><span style="color: #000000"> name;
}

</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setName(String name) {

    </span><span style="color: #0000ff">this</span>.name =<span style="color: #000000"> name;
}

</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span><span style="color: #000000"> getAge() {

    </span><span style="color: #0000ff">return</span><span style="color: #000000"> age;
}

</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setAge(<span style="color: #0000ff">int</span><span style="color: #000000"> age) {

    </span><span style="color: #0000ff">this</span>.age =<span style="color: #000000"> age;
}

}

package no.integrasco.ingentia.news.qaedition;

import java.util.List;

import net.sf.json.JSONArray; import net.sf.json.JsonConfig;

public class JsonTest {

</span><span style="color: #008000">/**</span><span style="color: #008000">
 * </span><span style="color: #808080">@param</span><span style="color: #008000"> args
 </span><span style="color: #008000">*/</span>
<span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> main(String[] args) {

    </span><span style="color: #008000">//</span><span style="color: #008000"> 转换方法1</span>
    JSONArray array = JSONArray.fromObject("[{'name':'hehe','age':22}]"<span style="color: #000000">);
    List</span>&lt;Person&gt; list = JSONArray.toList(array, Person.<span style="color: #0000ff">class</span>);<span style="color: #008000">//</span><span style="color: #008000"> 过时方法</span>
    System.out.println(list.get(0<span style="color: #000000">).getName());

    </span><span style="color: #008000">//</span><span style="color: #008000"> 转换方法2</span>
    List&lt;?&gt; list2 = JSONArray.toList(array, <span style="color: #0000ff">new</span> Person(), <span style="color: #0000ff">new</span> JsonConfig());<span style="color: #008000">//</span><span style="color: #008000">参数1为要转换的JSONArray数据,参数2为要转换的目标数据,即List盛装的数据</span>
    Person person = (Person) list2.get(0<span style="color: #000000">);
    System.out.println(person.getAge());
}

}

JSONArray数据转换成java List

2.从页面接收json格式的数据,在java类里面进行解析

JSONArray数据转换成java List
String jsonStr = {"name":"zfj","dd":\[{"ddr":"1","encrypt":"2","ff":"1","length":"23","ffe":"editStyled","ill":"1","pkor":"2","name":"zfj","isKey":"2","alias":"ffff"}\],"addRelations":\[{"type":"2","ld":"zfj","ld":"2"}\]};

//将此字符串放在ruquest或者其他的变量里面,传递到后台。

//如放在reques中: request.getRequestDispatcher("/geServlet?data="+jsonStr).forward(request,response);

//在servlet或java类里这样接收: String json = request.ge[size=large][/size]tParameter("data"); JSONObject jsonObject = JSONObject.fromObject(data); String name = jsonObject.getString(“name”); ...... //对于数组这样接收用 JSONArray jan = (JSONArray) jsonObject.get("dd"); if(jan!=null||jan.size()!=0){ for(int i=0;i<ja.size();i++){ JSONObject jo = JSONObject.fromObject(ja.get(i)); String ff = jo.getString("ff"); ...... } }

JSONArray数据转换成java List
JSONArray数据转换成java List
String jsonStr = {"name":"zfj","dd":\[{"ddr":"1","encrypt":"2","ff":"1","length":"23","ffe":"editStyled","ill":"1","pkor":"2","name":"zfj","isKey":"2","alias":"ffff"}\],"addRelations":\[{"type":"2","ld":"zfj","ld":"2"}\]};

//将此字符串放在ruquest或者其他的变量里面,传递到后台。

//如放在reques中: request.getRequestDispatcher("/geServlet?data="+jsonStr).forward(request,response);

//在servlet或java类里这样接收: String json = request.getParameter("data"); JSONObject jsonObject = JSONObject.fromObject(data); String name = jsonObject.getString(“name”); ...... //对于数组这样接收用 JSONArray jan = (JSONArray) jsonObject.get("dd"); if(jan!=null||jan.size()!=0){ for(int i=0;i<ja.size();i++){ JSONObject jo = JSONObject.fromObject(ja.get(i)); String ff = jo.getString("ff"); ...... } }

JSONArray数据转换成java List

 

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
java native方法与JNI实现
<divid"cnblogs\_post\_body"class"blogpostbody"<p<spanstyle"color:ff0000"<strongnative方法定义:</strong</span&nbsp;&nbsp;&nbsp;&nbsp;</p<p&nbsp;&nbsp;&nbsp;简单地讲
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
java连接neo4j
<divid"cnblogs\_post\_body"class"blogpostbody"<p呼、博客要落灰了</p<p记录一下Java连接neo4j的问题。</p<p<spanstyle"fontsize:16px;"<strong首先是neo4j的下载和配置,基本参考https://blog.csdn.net/ap
Wesley13 Wesley13
3年前
java 反射得到属性与属性值
<divid"cnblogs\_post\_body"class"blogpostbody"<p反射可以破坏所有的封装性,比如这次通过反射得到的属性与属性值;</p<divclass"cnblogs\_code"<divclass"cnblogs\_code\_toolbar"<spanclass"cnblogs\_co
Easter79 Easter79
3年前
Springboot+shiro配置笔记+错误小结(转)
<divclass"postBody"<divid"cnblogs\_post\_body"class"blogpostbody"<p  <spanstyle"color:ff0000"软件152尹以操</span</p<p  springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢
Wesley13 Wesley13
3年前
JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
<divid"cnblogs\_post\_body"class"blogpostbody"<h3加密代码</h3<p </p<divclass"cnblogs\_Highlightershgutter"<div<divid"highlighter\_969460"class"syntaxhighlighter
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
3年前
Elasticsearch基本概念及核心配置文件详解
<divid"cnblogs\_post\_body"class"blogpostbody"<p&nbsp;  Elasticsearch5.X,下列的是Elasticsearch2.X系类配置,其实很多配置都是相互兼容的</p<h2id"1配置文件"1.配置文件</h2<prename"code"<codeclass
Stella981 Stella981
3年前
Neo4j百万级数据导入只需30s
<divid"cnblogs\_post\_body"class"blogpostbodycnblogsmarkdown"<divclass"showcontentfree"<p先上图:425万nodes、180万relationships只用了30s243ms</p<divclass"imagepackage"