简体   繁体   English

JSON 数组到 Java 对象

[英]JSON Array to Java objects

I need to parse a JSON file which looks like this:我需要解析一个如下所示的 JSON 文件:

[
  {
    "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:我想把这些 X 坐标和 Y 坐标放到一个 JavaObject Click 中,那个类看起来像这样:

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 don't get it how I can do it from my file.我看过 gson,因为他们说它很简单,但我不知道如何从我的文件中做到这一点。

assuming your json string data is stored in variable called jsonStr :假设您的 json 字符串数据存储在名为jsonStr的变量中:

String jsonStr = getJsonFromSomewhere();
Gson gson = new Gson();
Click clicks[] = gson.fromJson(jsonStr, Click[].class);

Check out the Gson API and some examples.查看 Gson API 和一些示例。 I've put the links below!我已经把链接放在下面了!

String jsonString = //your json String
Gson gson = new Gson();
Type typeOfList = new TypeToken<List<Map<String, Integer>>>() {}.getType();
List<Map<String, Integer>> list = gson.fromJson(jsonString, typeOfMap);

List<Click> clicks = new ArrayList<Click>();
for(int i = 0; i < list.size(); i++) {
    int x = list.get(i).get("x");
    int y = list.get(i).get("y");
    clicks.add(new Click(x, y));
}

( http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html ) ( http://google-gson.googlecode.com/svn/tags/1.5/src/test/java/com/google/gson/functional/MapTest.java ) ( http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html ) ( http://google-gson.googlecode.com/svn/tags/ 1.5/src/test/java/com/google/gson/functional/MapTest.java )

Another solid option is Jackson it seems to have a pretty solid tutorial.另一个可靠的选择是Jackson ,它似乎有一个非常可靠的教程。 I'm not to familiar with it, so I hope this helps.我不太熟悉它,所以我希望这会有所帮助。

The main idea is that it uses an object mapper主要思想是它使用对象映射器

ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(new File("c:\\user.json"), User.class);

Step 4 should be your best bet, just realize you probably want something other than User.class第 4 步应该是你最好的选择,只要意识到你可能想要 User.class 以外的东西

EDIT:编辑:

If you're pretty set on using Gson, perhaps looking at other similar answers will help.如果您非常喜欢使用 Gson,那么查看其他类似的答案可能会有所帮助。 This question is about converting JSON to POJO (Plain Old Java Objects) and their are a few more like it floating around. 这个问题是关于将 JSON 转换为 POJO(普通旧 Java 对象)的,它们中的一些更像是浮动的。 Again, I'm not super familiar with these and I can try to answer some questions, but I hope this gets you where you need to go.同样,我对这些不是很熟悉,我可以尝试回答一些问题,但我希望这能让你到达你需要去的地方。

Happy coding.快乐的编码。 Leave a comment if you have any questions.如果您有任何问题,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM