Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
43
Task/JSON/Java/json.java
Normal file
43
Task/JSON/Java/json.java
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import com.google.gson.Gson;
|
||||
|
||||
public class JsonExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Gson gson = new Gson();
|
||||
String json = "{ \"foo\": 1, \"bar\": [ \"10\", \"apples\"] }";
|
||||
|
||||
MyJsonObject obj = gson.fromJson(json, MyJsonObject.class);
|
||||
|
||||
System.out.println(obj.getFoo());
|
||||
|
||||
for(String bar : obj.getBar()) {
|
||||
System.out.println(bar);
|
||||
}
|
||||
|
||||
obj = new MyJsonObject(2, new String[] { "20", "oranges" });
|
||||
json = gson.toJson(obj);
|
||||
|
||||
System.out.println(json);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MyJsonObject {
|
||||
|
||||
private int foo;
|
||||
private String[] bar;
|
||||
|
||||
public MyJsonObject(int foo, String[] bar) {
|
||||
this.foo = foo;
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public int getFoo() {
|
||||
return foo;
|
||||
}
|
||||
|
||||
public String[] getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue