字符串数组格式转换

该代码段展示了一个Java程序,用于解析包含JSON数据的字符串,特别是提取字段`ratio_list`,将其从字符串形式转换为JSON格式,并进行循环读取。程序首先创建JSONObject,然后遍历`DataRows`数组,对每个对象的`ratio_list`字段进行处理,去除转义字符并转换为JSONArray。之后,程序将这些数据存储到一个自定义的对象列表中。
摘要由CSDN通过智能技术生成

1、Json数据格式模板样例

{
    "RetVal":"1",
    "TotalCount":"2",
    "flag":"0",
    "RetCode":"1",
    "RowCount":"10",
    "DataRows":[
        [
            {
                "staff_name":"XXXX",
                "ratio_list":"[{\"proj_name\":\"项目名称\",\"proj_inter_code\":\"YGI21ID0000002\",\"proj_type\":\"ZB\",\"proj_expend_expenses_type\":\"C研发项目\",\"proj_ratio\":\"1.00\",\"proj_percent\":\"100.00\",\"is_lock\":\"未确认\"},{\"proj_name\":\"公共项目\",\"proj_inter_code\":\"0000000000000\",\"proj_type\":\"\",\"proj_expend_expenses_type\":\"\",\"proj_ratio\":\"0.00\",\"proj_percent\":\"0.00\",\"is_lock\":\"未确认\"}]",
                "staff_id":"0339975",
                "is_lock":"未确认",
                "status":"0"
            },
            {
                "staff_name":"XXXX",
                "ratio_list":"[{\"proj_name\":\"项目名称\",\"proj_inter_code\":\"YGI21UA0000007\",\"proj_type\":\"ZB\",\"proj_expend_expenses_type\":\"研发项目\",\"proj_ratio\":\"0.73\",\"proj_percent\":\"73.00\",\"is_lock\":\"未确认\"},{\"proj_name\":\"公共项目\",\"proj_inter_code\":\"0000000000000\",\"proj_type\":\"\",\"proj_expend_expenses_type\":\"\",\"proj_ratio\":\"0.27\",\"proj_percent\":\"27.00\",\"is_lock\":\"未确认\"}]",
                "staff_id":"0340160",
                "is_lock":"未确认",
                "status":"0"
            }
        ]
    ]
}

2、解析以上json数据单独获取字段“ratio_list” 字符串数组转换json格式循环读取数据

package timequery;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class test {
    public static void main(String[] args) {

        JSONObject Object = new JSONObject();
        JSONObject jObjectRet = new JSONObject("{"RetVal":"1","TotalCount":"2","flag":"0","RetCode":"1","RowCount":"10","DataRows":[[{"staff_name":"XXXX","ratio_list":"[{\\\"proj_name\\\":\\\"项目名称\\\",\\\"proj_inter_code\\\":\\\"YGI21ID0000002\\\",\\\"proj_type\\\":\\\"ZB\\\",\\\"proj_expend_expenses_type\\\":\\\"研发项目\\\",\\\"proj_ratio\\\":\\\"1.00\\\",\\\"proj_percent\\\":\\\"100.00\\\",\\\"is_lock\\\":\\\"未确认\\\"},{\\\"proj_name\\\":\\\"公共项目\\\",\\\"proj_inter_code\\\":\\\"0000000000000\\\",\\\"proj_type\\\":\\\"\\\",\\\"proj_expend_expenses_type\\\":\\\"\\\",\\\"proj_ratio\\\":\\\"0.00\\\",\\\"proj_percent\\\":\\\"0.00\\\",\\\"is_lock\\\":\\\"未确认\\\"}]","staff_id":"0000","is_lock":"未确认","status":"0"},{"staff_name":"XXXX","ratio_list":"[{\\\"proj_name\\\":\\\"项目名称\\\",\\\"proj_inter_code\\\":\\\"YGI21UA0000007\\\",\\\"proj_type\\\":\\\"ZB\\\",\\\"proj_expend_expenses_type\\\":\\\"研发项目\\\",\\\"proj_ratio\\\":\\\"0.73\\\",\\\"proj_percent\\\":\\\"73.00\\\",\\\"is_lock\\\":\\\"未确认\\\"},{\\\"proj_name\\\":\\\"公共项目\\\",\\\"proj_inter_code\\\":\\\"0000000000000\\\",\\\"proj_type\\\":\\\"\\\",\\\"proj_expend_expenses_type\\\":\\\"\\\",\\\"proj_ratio\\\":\\\"0.27\\\",\\\"proj_percent\\\":\\\"27.00\\\",\\\"is_lock\\\":\\\"未确认\\\"}]","staff_id":"11111","is_lock":"未确认","status":"0"}]]}");
        int num = 0;
        if (jObjectRet.has("DataRows")) {
            ArrayList array = new ArrayList();
            JSONArray dataRows = jObjectRet.getJSONArray("DataRows");
            for (int i = 0; i < dataRows.length(); i++) {

                JSONObject rowsJSONObject = dataRows.getJSONObject(i);
                String staffId = rowsJSONObject.getString("staff_id"); 
                String staffName = rowsJSONObject.getString("staff_name"); 
                String deptName = "";
                if(rowsJSONObject.has("dept_name")) {
                    deptName = rowsJSONObject.getString("dept_name");  
                }
                if(0!=rowsJSONObject.getString("ratio_list").length()) {

                    String ratio = rowsJSONObject.getString("ratio_list");
                    ratio = ratio.replaceAll("\\\\", "");
                    ratio = ratio.replaceAll("\"\\{", "\\{");
                    ratio = ratio.replaceAll("}\"", "\\}");
                    ratio = ratio.replaceAll("\"\\[", "\\[");
                    ratio = ratio.replaceAll("]\"", "\\]");
                    System.out.println(ratio);

                    JSONArray ratioList = new JSONArray(ratio);

                    for (int k = 0; k < ratioList.length(); k++) {

                        JSONObject rowsRatio = ratioList.getJSONObject(k);
                        timeExprotEntity entity = new timeExprotEntity();

                        String projName = rowsRatio.getString("proj_name");  
                        String projInterCode = rowsRatio.getString("proj_inter_code");  
                        String projType = rowsRatio.getString("proj_type"); 
                        String projExpendExpensesType = rowsRatio.getString("proj_expend_expenses_type");  
                        String isLock = rowsRatio.getString("is_lock"); 
                        String projRatio = rowsRatio.getString("proj_ratio"); 
                        // 循环获取值存入list
                        entity.setNum(String.valueOf(++num));
                        entity.setStaffId(staffId);
                        entity.setStaffName(staffName);
                        entity.setDeptName(deptName);
                        entity.setProjName(projName);
                        entity.setProjInterCode(projInterCode);
                        entity.setProjType(projType);
                        entity.setProjExpendExpensesType(projExpendExpensesType);
                        entity.setIsLock(isLock);
                        entity.setProjRatio(projRatio);
                        array.add(entity);
                    }
                }
            }
            jObjectRet.put("DataRows",array);
            System.out.println(jObjectRet);
        }
    }
}

3、以上代码段中主要以转换数据并存入数据为主

针对字段 “ratio_list” 通过以下替换方式处理

                    String ratio = rowsJSONObject.getString("ratio_list");
                    ratio = ratio.replaceAll("\\\\", "");
                    ratio = ratio.replaceAll("\"\\{", "\\{");
                    ratio = ratio.replaceAll("}\"", "\\}");
                    ratio = ratio.replaceAll("\"\\[", "\\[");
                    ratio = ratio.replaceAll("]\"", "\\]");
                    System.out.println(ratio);

输出值为:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值