JSON数组到Java对象 [英] JSON Array to Java objects
本文介绍了JSON数组到Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要解析一个JSON文件至极看起来是这样的:
I need to parse a json file wich looks like this:
[
{
"y": 148,
"x": 155
},
{
"y": 135,
"x": 148
},
{
"y": 148,
"x": 154
}
]
和我想要把这些的X坐标和Y坐标成JAVAOBJECT点击,该类看起来像这样:
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;
}
}
我已经看过GSON,因为他们说,这是戒烟容易,但我不明白我怎么能在我的文件做到这一点。
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.
推荐答案
假设你的JSON字符串的数据被存储在一个名为变量 jsonStr
:
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);
这篇关于JSON数组到Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文