jsonparser java code example
Example 1: arrays.fill java
import java.util.Arrays;
int[] myArray = new int[10];
Arrays.fill(myArray,1);
Example 2: how to parse json in java
import org.json.*;
String jsonString = ... ;
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 3: java parse json
Gson gson = new Gson();
gson.toJson(yourObject);
yourObject o = gson.fromJson(JSONString,yourObject.class);
Example 4: json parse java
public static void main(String[] args) throws Exception {
JSONObject jsonObject = (JSONObject) readJsonSimpleDemo("example.json");
System.out.println(jsonObject);
System.out.println(jsonObject.get("age"));
}
public static Object readJsonSimpleDemo(String filename) throws Exception {
FileReader reader = new FileReader(filename);
JSONParser jsonParser = new JSONParser();
return jsonParser.parse(reader);
}
Example 5: java "->"
Runnable r = ()-> System.out.print("Run method");
Runnable r = new Runnable() {
@Override
public void run() {
System.out.print("Run method");
}
};