soft

soft

JAVA JSONObject.parseArray(str) value中双引号引起的解析失败

public static void main(String[] args){
    String aaa="[{\"industry_image\":\"/uploadfile/02af887979a8414588dc51c8cd10eb2c.jpg\",\"robot_title\":\"Automation in 3C Industry\",\"industry_desc\":\"The electronics industry is an important innovation industry that needs the support of innovation. No matter electronics which contain computer chips, calculators, digital cameras, LCD displays, sensing technologies, DVD players, MP5, fuel cells, etc., or 3C which contains mobile phone computer housings and keys, panels, window lenses, etc., FANUC robots are committed to improving manufacturing processes and production efficiency for IT electronics (computers, communications and consumer electronics) manufacturers.\"},{\"industry_image\":\"/uploadfile/3a09d949911e443d9a175a897009b8b4.png\",\"robot_title\":\"Robots in 3C Industry\",\"industry_desc\":\"FANUC robot, which is famous for its precision, high speed and high efficiency, can accurately operate and locate precision components and carry out efficient system application under the condition of ensuring high repetition accuracy, thus becoming the preferred product in the electronic industry. At present, FANUC robot family has \"entered\" factories all over the world and is an indispensable \"backbone\" of production in assembly, spraying, workpiece handling, processing, and testing, etc.\"}]\n";
    String bbb=toJsonString(aaa);
    System.out.println(JSONObject.parseArray(bbb));;
    System.out.println("xxxxx");
}
    
// 处理json字符串中value多余的双引号, 将多余的双引号替换为中文双引号
private static String toJsonString(String s) {
    char[] tempArr = s.toCharArray();
    int tempLength = tempArr.length;
    for (int i = 0; i < tempLength; i++) {
        if (tempArr[i] == ':' && tempArr[i + 1] == '"') {
            for (int j = i + 2; j < tempLength; j++) {
                if (tempArr[j] == '"') {
                    if (tempArr[j + 1] != ',' && tempArr[j + 1] != '}') {
                        tempArr[j] = '”'; // 将value中的 双引号替换为中文双引号
                    } else if (tempArr[j + 1] == ',' || tempArr[j + 1] == '}') {
                        break;
                    }
                }
            }
        }
    }
    return new String(tempArr);
}

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

联系我 331434376    15629529961