转自:https://blog.csdn.net/harryptter/article/details/54575577
Java单个对象和List对象转换成Json,Json转List 可查看上一篇文章:https://blog.csdn.net/qq_36411874/article/details/83114337
目录
4,java解析提取json指定节点里面的数组字符串中存入List数组
备注:格式化检测json网址:
备注:导入的jar包
import java.util.List;
import org.junit.Test;
import com.suning.api.DefaultSuningClient;
import com.suning.api.entity.custom.orderGetGetRequest;
import com.suning.api.entity.custom.orderGetGetResponse;
import com.suning.api.entity.transaction.OrdercodeQueryRequest;
import com.suning.api.entity.transaction.OrdercodeQueryResponse;
import com.suning.api.exception.SuningApiException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
1.简单的解析json字符串
首先将json字符串转换为json对象,然后再解析json对象,过程如下。
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
根据json中的键得到它的值
String name = jsonObject.getString("name");
int num = jsonObject.getInt("num");
String sex = jsonObject.getString("sex");
int age = jsonObject.getInt("age");
2.将json字符串转换为java对象
同样先将json字符串转换为json对象,再将json对象转换为java对象,如下所示。
JSONObject obj = new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象
将json对象转换为java对象
Person jb = (Person)JSONObject.toBean(obj,Person.class);//将建json对象转换为Person对象
3.将java对象转换为json字符串
先将java对象转换为json对象,在将json对象转换为json字符串
JSONObject json = JSONObject.fromObject(obj);//将java对象转换为json对象
String str = json.toString();//将json对象转换为字符串
4,java解析提取json指定节点里面的数组字符串中存入List数组
{\"sn_responseContent\":{\"sn_body\":{\"orderCodeQuery\":{\"orderCode\":[\"346\",\"333\",\"364\",\"361\",\"361\",\"323\",\"394\",\"387\",\"324\",\"393\"]}},\"sn_head\":{}}}
第一版本:
JSONObject orderCodejson= JSONObject.fromObject(orderCodeQuery);//解析最外层orderCode
JSONArray jsonArray = orderCodejson.getJSONArray("orderCode");
List<String> sList = JSONArray.toList(jsonArray,String.class);(备注,这里的类已经过时,不要用这种,详情往下看第二第三版本)
try {
JSONObject jsonObject= JSONObject.fromObject(response.getBody());//解析最外层sn_responseContent
String sn_responseContent = jsonObject.getString("sn_responseContent");
JSONObject sn_bodyjson= JSONObject.fromObject(sn_responseContent);//解析最外层sn_body
String sn_body = sn_bodyjson.getString("sn_body");//解析外层sn_body
JSONObject orderCodeQueryjson= JSONObject.fromObject(sn_body);//解析最外层
String orderCodeQuery = orderCodeQueryjson.getString("orderCodeQuery");//解析外层orderCodeQuery
JSONObject orderCodejson= JSONObject.fromObject(orderCodeQuery);//解析最外层orderCode
JSONArray jsonArray = orderCodejson.getJSONArray("orderCode");
List<String> sList = JSONArray.toList(jsonArray,String.class);
for(String s : sList){
System.out.println(s);
}
} catch (Exception e) {
System.out.println("出现异常");
e.printStackTrace();
}
第二版本:
用单元测试,代码如下。看起来优化简洁了很多。(但是,感觉还是太长太啰嗦,继续优化,看第三版本,改进终极版本)
@Test
public void test() {
String json = "{\"sn_responseContent\":{\"sn_body\":{\"orderCodeQuery\":{\"orderCode\":[\"346\",\"333\",\"364\",\"361\",\"361\",\"323\",\"394\",\"387\",\"324\",\"393\"]}},\"sn_head\":{}}}";
JSONObject jsonObject = JSONObject.fromObject(json);
JSONObject sn_res = jsonObject.getJSONObject("sn_responseContent");
JSONObject sn_body = sn_res.getJSONObject("sn_body");
JSONObject query = sn_body.getJSONObject("orderCodeQuery");
//JSONObject orderCode = query.getJSONObject("orderCode");
JSONArray jsonArray = query.getJSONArray("orderCode");
//List<String> sList = JSONArray.toList(jsonArray,String.class);//丢弃用法
List<String> sList = JSONArray.toList(jsonArray,new String(), new JsonConfig());
for(String s : sList){
System.out.println(s);
}
}
第三版本:
终极版本,美观而且看起来心旷神怡的版本。
@Test
public void test2() {//改进版本
String json = "{\"sn_responseContent\":{\"sn_body\":{\"orderCodeQuery\":{\"orderCode\":[\"346\",\"333\",\"364\",\"361\",\"361\",\"323\",\"394\",\"387\",\"324\",\"393\"]}},\"sn_head\":{}}}";
JSONObject jsonObject = JSONObject.fromObject(json);
JSONArray jsonArray = jsonObject.getJSONObject("sn_responseContent")
.getJSONObject("sn_body")
.getJSONObject("orderCodeQuery")
.getJSONArray("orderCode");
List<String> sList = JSONArray.toList(jsonArray,new String(), new JsonConfig());
for(String s : sList){
System.out.println(s);
}
}
完整代码
package baz.parse;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import baz.bean.Person;
public class ParseJson {
private String jsonStr;
public ParseJson() {
}
public ParseJson(String str){
this.jsonStr = str;
}
/**
* 解析json字符串
*/
public void parse(){
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
String name = jsonObject.getString("name");
int num = jsonObject.getInt("num");
String sex = jsonObject.getString("sex");
int age = jsonObject.getInt("age");
System.out.println(name + " " + num + " " + sex + " " + age);
}
//将json字符串转换为java对象
public Person JSON2Object(){
//接收{}对象,此处接收数组对象会有异常
if(jsonStr.indexOf("[") != -1){
jsonStr = jsonStr.replace("[", "");
}
if(jsonStr.indexOf("]") != -1){
jsonStr = jsonStr.replace("]", "");
}
JSONObject obj = new JSONObject().fromObject(jsonStr);//将json字符串转换为json对象
Person jb = (Person)JSONObject.toBean(obj,Person.class);//将建json对象转换为Person对象
return jb;//返回一个Person对象
}
}
实体类
package baz.bean;
public class Person {
private String name;
private int num;
private String sex;
private int age;
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String name, int num, String sex, int age) {
super();
this.name = name;
this.num = num;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
将java对象转换为json字符串
package baz.cons;
import net.sf.json.JSONObject;
/**
* 将java对象转换为json字符串
* @author Administrator
*
*/
public class ConsJson {
public ConsJson() {
// TODO Auto-generated constructor stub
}
public String Object2Json(Object obj){
JSONObject json = JSONObject.fromObject(obj);//将java对象转换为json对象
String str = json.toString();//将json对象转换为字符串
return str;
}
}
测试类:
package baz.test;
import java.util.List;
import baz.bean.Person;
import baz.cons.ConsJson;
import baz.parse.ParseJson;
public class Test {
public static void main(String[] args) {
//将字符串转换为json对象,然后根据建得到相应的值
ParseJson pj = new ParseJson("{\"name\":\"gu\",\"num\":123456,\"sex\":\"male\",\"age\":24}");
pj.parse();
//将一个json字符串转换为java对象
Person p = pj.JSON2Object();
System.out.println("Name:" + p.getName());
System.out.println("Num:" + p.getNum());
System.out.println("Sex:" + p.getSex());
System.out.println("age:" + p.getAge());
//将一个java对象转换为Json字符串
Person p1 = new Person("gu1",123,"male",23);
ConsJson cj = new ConsJson();
String str1 = cj.Object2Json(p1);
System.out.println(str1);
}
}