zoukankan      html  css  js  c++  java
  • 使用JSONArray出现{"$ref":"$.certPicInfos[0]"}异常的解决方法

    这次做接口时,对方把图片类型和图片地址用的是数组,所有我需要用JSONArray做拼接,使用的过程中出现了{"$ref":"$.certPicInfos[0]"}异常。

    public void test3() {
    JSONObject json = new JSONObject();
    String picUrls = "1|test1.png;2|test2.png";
    JSONArray jsonArr = new JSONArray();
    String[] certPicUrls = picUrls.split(";");
    JSONObject para = new JSONObject();//出错代码
    for (String certPicUrl : certPicUrls) {
    String picKey = certPicUrl.substring(0, certPicUrl.indexOf("|"));
    String picValue = certPicUrl.substring(certPicUrl.indexOf("|") + 1, certPicUrl.length());
    para.put("certPicType", picKey);
    para.put("certPicUrl", picValue);
    jsonArr.add(para);
    }
    json.put("certPicInfos", jsonArr);
    System.out.println(json);
    }

    出错原因:JSONObject对象被引用了两次。

    解决方法:将JSONObject的创建放到循环体内

    public void test3() {

    JSONObject json = new JSONObject();
    String picUrls = "1|test1.png;2|test2.png";
    JSONArray jsonArr = new JSONArray();
    String[] certPicUrls = picUrls.split(";");
    JSONObject para = new JSONObject();
    for (String certPicUrl : certPicUrls) {

    JSONObject para = new JSONObject();//正确代码

    String picKey = certPicUrl.substring(0, certPicUrl.indexOf("|"));
    String picValue = certPicUrl.substring(certPicUrl.indexOf("|") + 1, certPicUrl.length());
    para.put("certPicType", picKey);
    para.put("certPicUrl", picValue);
    jsonArr.add(para);
    }
    json.put("certPicInfos", jsonArr);
    System.out.println(json);
    }

  • 相关阅读:
    datetime模块
    time模块
    shelve模块
    json&pickle 序列化
    re正则
    MQ常用命令
    MQ for linux安装与卸载【转】
    Linux下安装Oracle11g服务器【转】
    PLSQL_数据泵Datapump导入导出数据IMPDP / EXPDP(概念)(Oracle数据导入导出工具)[转]
    [LeetCode]:116:Populating Next Right Pointers in Each Node
  • 原文地址:https://www.cnblogs.com/lucky-girl/p/9364864.html
Copyright © 2011-2022 走看看