简体   繁体   English

如何在Java中解析二维JSON数组

[英]How to parse a two dimensional JSON array in java

This is my json data 这是我的json数据

 {
    "tag1":["haaai ","hello"],

    "tag2":[["haai1","haai2"],["hello1","hello2","hello3"]]
 }

I can successfully read array tag1 using my java code 我可以使用我的Java代码成功读取数组tag1

 JSONArray msg = (JSONArray) jsonObject.get("tag1");    
 Iterator<String> iterator = msg.iterator();

 while (iterator.hasNext()) {    
        String text = iterator.next();    
        System.out.println(text);
 }

But, I can't read the tag2 array using the above method. 但是,我无法使用上述方法读取tag2数组。 Any idea? 任何想法?

You can't read using the same method because the format is different. 您无法使用相同的方法阅读,因为格式不同。 You will have to use 2 loops instead. 您将不得不使用2个循环。

 JSONArray msg = (JSONArray) jsonObject.get("tag2");    
 Iterator<JSONArray> iterator = msg.iterator();

 while (iterator.hasNext()) {
        Iterator<String> innerIterator = iterator.next().iterator();
        while (innerIterator.hasNext()) {
             String text = innerIterator.next();    
             System.out.println(text);
        }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM