如何用直接值解析数组,两次json编码

时间:2014-09-17 07:56:35

标签: java arrays json parsing

如何使用直接值解析数组,在Java中编码两次json,我将数据作为字符串获取,并且我想从多维数组中获取每个值。

我是一个关于java的菜鸟,我设法拉出一个不那么优雅的解决方案,遇到问题,当我分开“,”如果里面的文字有“,”我可以用正则表达式但是必须有比这更优雅的解决方案:

    content = the data fetched from the api as a string
content = content.replace("\"[[", "[");
            content = content.replace("]]\"", "]");
            content = content.replaceAll("\\\\","");
             for (String FaData : content.split("\\],\\[")) {
                for (String FaDataData : FaData.split(",")) {
                    FaDataData.toString();
                }
            }

这里有一个示例,说明内容字符串在获取时的实际外观:

"[[308576,1410880665,162506,\"Bobcat\",1,\"http:\\\/\\\/hugelolcdn.com\\\/i460\\\/308576.jpg\",\"Well no\",82,3,\"\"],[308592,1410883832,9479,\"undeathkiller\",2,\"http:\\\/\\\/hugelolcdn.com\\\/i\\\/308592.gif\",\"Guess the stupidity level\",89,9,\"\"],[308574,1410879991,32277,\"rady123lol\",2,\"http:\\\/\\\/hugelolcdn.com\\\/i\\\/308574.gif\",\"force of habit\",92,3,\"\"],[308624,1410897686,149704,\"Raptide7\",1,\"http:\\\/\\\/hugelolcdn.com\\\/i460\\\/308624.jpg\",\"*breathing intensifies*\",114,8,\"\"],[308648,1410911037,114669,\"Huller\",1,\"http:\\\/\\\/hugelolcdn.com\\\/i460\\\/308648.jpg\",\"SPOILERS: Stannis kills Dumbledore\",133,2,\"\"],[308628,1410898654,135315,\"Mig_L\",1,\"http:\\\/\\\/hugelolcdn.com\\\/i460\\\/308628.jpg\",\"So badass\",117,2,\"gold\"],[308639,1410902872,62886,\"burningowl\",1,\"http:\\\/\\\/hugelolcdn.com\\\/i460\\\/308639.jpg\",\"Kid's going places yo\",125,4,\"\"],[308520,1410858123,73400,\"koppie888\",1,\"http:\\\/\\\/hugelolcdn.com\\\/i460\\\/308520.jpg\",\"4chan, what a beautifull place\",99,7,\"\"],[308546,1410872801,32277,\"rady123lol\",1,\"http:\\\/\\\/hugelolcdn.com\\\/i460\\\/308546.jpg\",\"( \\u0361\\u00b0 \\u035c\\u0296 \\u0361\\u00b0)\",118,17,\"\"],[308486,1410846601,176339,\"AtLeastISubmit\",1,\"http:\\\/\\\/hugelolcdn.com\\\/i460\\\/308486.jpg\",\"That 70's show called it.\",101,3,\"\"]]"

3 个答案:

答案 0 :(得分:0)

试试这个

content = content.trim();
content = content.substring(0, content.length()); //gets the length of content string
content = content.replaceAll("\\/","/"); //Replaces all \/ to /

它也适用于括号。

答案 1 :(得分:0)

如果您正在使用JSON,那么我建议您使用JSON库,例如jackson

ObjectMapper mapper = new ObjectMapper();
String[][] 2Darray = mapper.readValue(content, String[][].class);

但是,如果您使用的是JSON,那么如果您的数据格式更加结构化将会更好。显然,这取决于您是否对API有任何控制权。

答案 2 :(得分:0)

假设您的文本在名为所有的字符串变量中,使用JSONSimple package,您可以使用以下代码:

try {
    // create a new JSONParser
    JSONParser parser=new JSONParser();
    // first JSON decoding
    Object obj = parser.parse(everything);
    // second JSON decoding
    obj = parser.parse(obj.toString());
    // cast the parsed JSON string to a new JSONArray
    JSONArray array = (JSONArray)obj;
    // loop through each line of the initial JSONArray
    for (int i = 0; i < array.size(); i++){
        // write the array values as a single line
        System.out.println(i + " : " + array.get(i));
        // parsing each line as a new JSONArray
        JSONArray tmp = (JSONArray)parser.parse(array.get(i).toString());
        for (int j = 0; j < tmp.size(); j++){ // iterate over the parsed values
            System.out.println(i+"."+j+" : "+tmp.get(j));
        }
    }
} catch (ParseException ex) {
    Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}

当然,您还必须从JSON包中导入以下类:

import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;