I need to parse a json file wich looks like this:
[
{
"y": 148,
"x": 155
},
{
"y": 135,
"x": 148
},
{
"y": 148,
"x": 154
}
]
And I want to put these X-coordinates and Y-coordinates into an JavaObject Click, that class looks like this:
public class Click {
int x;
int y;
public Click(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
I have looked at gson because they say it is quit easy but i dont get it how I can do it from my file.
解决方案
assuming your json string data is stored in variable called jsonStr:
String jsonStr = getJsonFromSomewhere();
Gson gson = new Gson();
Click clicks[] = gson.fromJson(jsonStr, Click[].class);