如何在Codename One中读取嵌套的JSON

时间:2017-09-27 07:08:24

标签: codenameone

我一直按照这里的说明操作: https://www.codenameone.com/javadoc/com/codename1/io/JSONParser.html从json文件中检索值。 我已设法读取我的json内容的顶级值 - 但是我看不到如何读取嵌套标记的值,例如使用此文件......

{
"glossary":{
  "title":"example glossary",
  "GlossDiv":{
     "title":"S",
     "GlossList":{
        "GlossEntry":{
           "ID":"SGML",
           "SortAs":"SGML",
           "GlossTerm":"Standard Generalized Markup Language",
           "Acronym":"SGML",
           "Abbrev":"ISO 8879:1986",
           "GlossDef":{
              "para":"A meta-markup language, used to create markup languages such as DocBook.",
              "GlossSeeAlso":[
                 "GML",
                 "XML"
              ]
           },
           "GlossSee":"markup"
        }
     }
  }
}
}

有人可以告诉我如何获得上面“para”的价值吗?

谢谢

1 个答案:

答案 0 :(得分:3)

根据count()解析json数据后,您可以使用Result深入了解json内容:

import com.codename1.processing.Result;

...

Map<String, Object> data = json.parseJSON(r);
Result result = Result.fromContent(data);
String id = result.getAsString("glossary/GlossDiv/GlossList/GlossEntry/ID");
String para = result.getAsString("glossary/GlossDiv/GlossList/GlossEntry/GlossDef/para");