便宜VPS主机精选
提供服务器主机评测信息

java解析json报文文件(附详细代码)

前台一个json格式的数组,后台怎样写才能接收,并解释?先创建两个类:Test和testbean,首先自己弄一个类似的json字符串(需要自己转化一下格式哦):

map对象:{Status=200, Message=查询成功, Paging={
"PageSize":2,"TotalRecords":250,"PageIndex":1}, 
OrderNumber=ECI2019061819433684318720,
 Result=[
{"KeyNo":"4659626b1e5e43f1bcad8c268753216e",
"StartDate":"2012-07-10 00:00:00",
"Status":"在业",
"No":"110108015068911",
"CreditCode":"9111010859963405XW",
"OperName":"程维",
"Name":"北京小桔科技有限公司"},
{"KeyNo":"4178fc374c59a79743c59ecaf098d4dd",
"StartDate":"2015-04-22 00:00:00",
"Status":"存续",
"No":"440301112653267",
"CreditCode":"91440300334945450M",
"OperName":"王举",
"Name":"深圳市小桔科技有限公司"}
]
}

下面是两个类的内容:

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
 * @Author: sunsiny
 * @Date: 2020/11/11 9:15 (日期和时间)
 */
public class Test {
    private static final com.alibaba.fastjson.JSON JSON = null;
    public static void main(String[] args) {
        String jsondata = "{\"OrderNumber\":\"ECI2019061819433684318720\",\"Paging\":{\"PageSize\":2,\"PageIndex\":1,\"TotalRecords\":250},\"Result\":[{\"KeyNo\":\"4659626b1e5e43f1bcad8c268753216e\",\"Name\":\"北京小桔科技有限公司\",\"OperName\":\"程维\",\"StartDate\":\"2012-07-10 00:00:00\",\"Status\":\"在业\",\"No\":\"110108015068911\",\"CreditCode\":\"9111010859963405XW\"},{\"KeyNo\":\"4178fc374c59a79743c59ecaf098d4dd\",\"Name\":\"深圳市小桔科技有限公司\",\"OperName\":\"王举\",\"StartDate\":\"2015-04-22 00:00:00\",\"Status\":\"存续\",\"No\":\"440301112653267\",\"CreditCode\":\"91440300334945450M\"}],\"Status\":\"200\",\"Message\":\"查询成功\"}";
        JSONObject obj= JSON.parseObject(jsondata);
        //map对象
        Map<String, Object> data =new HashMap<String,Object>();
        //循环转换
        Iterator it =obj.entrySet().iterator();
        while (it.hasNext()) {
        
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            if("Result".equals(entry.getKey())) {
                JSONArray arr = (JSONArray) entry.getValue();
                List<testbean> list = JSONObject.parseArray(arr.toJSONString(),testbean.class);
                System.out.println("list数组:"+list);
                for(testbean testbean : list){
                    System.out.println(testbean);
                }
            }
        }
    }
}
/**
 * @Author: sunsiny
 * @Date: 2020/11/11 10:06 (日期和时间)
 */
public class testbean {
    String KeyNo;

    String Name;

    String OperName;

    String StartDate;

    String Status;

    String No;

    String CreditCode;

    public String getKeyNo() {
        return KeyNo;
    }

    public void setKeyNo(String keyNo) {
        KeyNo = keyNo;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getOperName() {
        return OperName;
    }

    public void setOperName(String operName) {
        OperName = operName;
    }

    public String getStartDate() {
        return StartDate;
    }

    public void setStartDate(String startDate) {
        StartDate = startDate;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }

    public String getNo() {
        return No;
    }

    public void setNo(String no) {
        No = no;
    }

    public String getCreditCode() {
        return CreditCode;
    }

    public void setCreditCode(String creditCode) {
        CreditCode = creditCode;
    }

    @Override
    public String toString() {
        return "testbean{" +
                "KeyNo='" + KeyNo + '\'' +
                ", Name='" + Name + '\'' +
                ", OperName='" + OperName + '\'' +
                ", StartDate='" + StartDate + '\'' +
                ", Status='" + Status + '\'' +
                ", No='" + No + '\'' +
                ", CreditCode='" + CreditCode + '\'' +
                '}';
    }
}

打印出来的内容是:

list数组:[testbean{KeyNo='4659626b1e5e43f1bcad8c268753216e', Name='北京小桔科技有限公司', OperName='程维', StartDate='2012-07-10 00:00:00', Status='在业', No='110108015068911', CreditCode='9111010859963405XW'}, testbean{KeyNo='4178fc374c59a79743c59ecaf098d4dd', Name='深圳市小桔科技有限公司', OperName='王举', StartDate='2015-04-22 00:00:00', Status='存续', No='440301112653267', CreditCode='91440300334945450M'}]
testbean{KeyNo='4659626b1e5e43f1bcad8c268753216e', Name='北京小桔科技有限公司', OperName='程维', StartDate='2012-07-10 00:00:00', Status='在业', No='110108015068911', CreditCode='9111010859963405XW'}
testbean{KeyNo='4178fc374c59a79743c59ecaf098d4dd', Name='深圳市小桔科技有限公司', OperName='王举', StartDate='2015-04-22 00:00:00', Status='存续', No='440301112653267', CreditCode='91440300334945450M'}

未经允许不得转载:便宜VPS测评 » java解析json报文文件(附详细代码)