-1

I have a josn like this.From this how can i retrive platfrom and version values using java

code

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",

    }
  ]

 }
Jayaram
  • 1,715
  • 18
  • 30
Psl
  • 3,830
  • 16
  • 46
  • 84

3 Answers3

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

Similarly, replace France24 with Seloger and Marmiton and repeat.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
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

Community
  • 1
  • 1
Jayaram
  • 1,715
  • 18
  • 30
  • @Psl, do you mean versionLst and platformLst is not getting populated? – Jayaram Jun 23 '14 at 09:02
  • getting error in place of ` JSONArray array = jsonObject.getJSONArray("France24");` – Psl Jun 23 '14 at 09:05
  • Shouldn't be...Can you please share the exception details? – Jayaram Jun 23 '14 at 09:07
  • `The method getJSONArray(String) is undefined for the type JSONObject Type mismatch: cannot convert from org.json.JSONObject to org.json.simple.JSONObject The method getString(String) is undefined for the type JSONObject The method getString(String) is undefined for the type JSONObject` – Psl Jun 23 '14 at 09:08
  • ok beacuse of you are using simple json where as you are importing org.json....check your import statment and correct those..also try using JSONArray msg = (JSONArray) jsonObject.get("France24"); – Jayaram Jun 23 '14 at 09:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56095/discussion-between-jayaram-pradhan-and-psl). – Jayaram Jun 23 '14 at 09:16
1

Like that:

JSONObject france = jsonObject.getJsonArray("France24").getJsonObject(0);
String platform = france.getString("platform");
String version = france.getString("version");
win_wave
  • 1,498
  • 11
  • 9