如何在Codename One中循环嵌套的JSON数据

时间:2017-09-28 07:34:34

标签: codenameone

这与此问题有关

How to read nested JSON in Codename One

(我不知道你是否应该编辑和存在问题???)

所以,多亏了这个问题的答案,我现在知道如何检索我需要的特定标签值,但不幸的是,如果数据循环,它不起作用......例如

我如何得到" para"的循环值?从这段代码?

{  
  "glossary":{  
  "title":"example glossary",
  "GlossDiv":{  
     "title":"S",
     "GlossList":{  
        "GlossEntry":{  
           "ID":"SGML1",
           "SortAs":"SGML1",
           "GlossTerm":"Standard Generalized Markup Language",
           "Acronym":"SGML1",
           "Abbrev":"ISO 8879:1986",
           "GlossDef":{  
              "para":"para1",
              "GlossSeeAlso":[  
                 "GML",
                 "XML"
              ]
           },
           "GlossSee":"markup"
        },
        "GlossEntry":{  
           "ID":"SGML2",
           "SortAs":"SGML2",
           "GlossTerm":"Standard Generalized Markup Language",
           "Acronym":"SGML2",
           "Abbrev":"ISO 8879:1986",
           "GlossDef":{  
              "para":"para2",
              "GlossSeeAlso":[  
                 "GML",
                 "XML"
              ]
           },
           "GlossSee":"markup"
        },
        "GlossEntry":{  
           "ID":"SGML3",
           "SortAs":"SGML3",
           "GlossTerm":"Standard Generalized Markup Language",
           "Acronym":"SGML3",
           "Abbrev":"ISO 8879:1986",
           "GlossDef":{  
              "para":"para3",
              "GlossSeeAlso":[  
                 "GML",
                 "XML"
              ]
           },
           "GlossSee":"markup"
        }
     }
  }
}
}

再次 - 感谢任何帮助

由于

1 个答案:

答案 0 :(得分:2)

遵循answer到先前question的模式,您可以这样做:

String path = "glossary/GlossDiv/GlossList"

int size = result.getSizeOfArray(path);
if (size > 0) {
    for (int i = 0; i < size; i++) {
        String para = result.getAsString(path + "[" + i + "]/GlossEntry/GlossDef/para");
        System.out.println(para);
    }
}