How to read nested JSON in Codename One

一笑奈何 提交于 2019-12-08 00:21:17

问题


I have been following the instructions here: https://www.codenameone.com/javadoc/com/codename1/io/JSONParser.html to retrieve a value from a json file. I have managed to read the top level value of my json content - however I cannot see how to read the value of a nested tag e.g. using this file ...

{
"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"
        }
     }
  }
}
}

Please can someone show me how to get to the value of "para" above?

Thanks


回答1:


After parsing your json data based on this, you can use Result to read deep into the json content:

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");


来源:https://stackoverflow.com/questions/46441538/how-to-read-nested-json-in-codename-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!