JSONParser java parse int code example

Example 1: how to parse json in java

import org.json.*;

String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts");
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......
}

Example 2: org.json.simple.JSONobject get integer

public static Update fromString(String json) {
    try {
      // Read json string
      JSONObject obj = (JSONObject) new JSONParser().parse(json);
      int buildNumber = ((Long) obj.get("buildNumber")).intValue();
      URL download = new URL((String) obj.get("download"));
      // Return cached update from json
      return new Update(buildNumber, download, true);
    } catch (IOException | ParseException ex) {
      Log.e(TAG, "Invalid JSON object: " + json);
    }
    // Shouldn't be returned, but it may happen
    return new Update(0, null, true);
  }
}

Tags:

Java Example