使用java json解析器解析json值

时间:2014-06-23 08:47:36

标签: java

我有一个像这样的josn。从这个我如何使用java

检索平台和版本值

public static void main(String[] args)
        throws org.json.simple.parser.ParseException {

    try {
        // read the json file
        FileReader reader = new FileReader(filePath);
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (NullPointerException ex) {
        ex.printStackTrace();
    }

}

josn

{
  "France24":[
    {
      "platform":"Linux",
      "version":"12.3",      
    }
  ],

   "Seloger":[
    {
      "platform":"windows",
      "version":"8",      
    }
  ],

  "Marmiton":[
   {
      "platform":"mac",
      "version":"10.1",

    }
  ]

 }

3 个答案:

答案 0 :(得分:1)

JSONArray jArray = jsonObject.getJSONArray("France24");
JSONObject france24Object = jArray.get(0);
String platform = france24Object.getString("platform");
String version = france24Object.getString("version");

同样,用Seloger和Marmiton取代France24并重复。

答案 1 :(得分:1)

List<String> platformLst = new ArrayList<String>();
List<String> versionLst = new ArrayList<String>();
JSONArray array = obj.getJSONArray("France24");
for(int i = 0 ; i < array.length() ; i++){
    JSONObject obj = array.getJSONObject(i);
    versionLst.add(obj.getString("platform"));
    platformLst .add(obj.getString("version"));
}

Existing Question

Example

Simple Json Tutorial Link

答案 2 :(得分:1)

就像那样:

JSONObject france = jsonObject.getJsonArray("France24").getJsonObject(0);
String platform = france.getString("platform");
String version = france.getString("version");