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);
    }

  • 相关阅读:
    bat脚本%cd%和%~dp0的区别
    java测试程序运行时间
    != 的注意事项
    [转载] iptables 防火墙设置
    .NET 创建 WebService
    [转载] 学会使用Web Service上(服务器端访问)~~~
    cygwin 安装 apt-cyg
    在Element节点上进行Xpath
    Element节点输出到System.out
    [转载] 使用StAX解析xml
  • 原文地址:https://www.cnblogs.com/lucky-girl/p/9364864.html
Copyright © 2011-2022 走看看