JSON 数组到 Java 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27628096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:44:17  来源:igfitidea点击:

JSON Array to Java objects

javajsonparsingarraylistgson

提问by ProblemAnswerQue

I need to parse a json file wich 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 dont get it how I can do it from my file.

我看过gson,因为他们说这很容易,但我不知道如何从我的文件中做到这一点。

采纳答案by Yazan

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);

回答by Matt

Another solid option is Hymansonit seems to have a pretty solid tutorial. I'm not to familiar with it, so I hope this helps.

另一个可靠的选择是Hymanson它似乎有一个非常可靠的教程。我不熟悉它,所以我希望这会有所帮助。

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. This questionis about converting JSON to POJO (Plain Old Java Objects) and their are a few more like it floating around. 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.

如果您非常喜欢使用 Gson,也许查看其他类似的答案会有所帮助。这个问题是关于将 JSON 转换为 POJO(Plain Old Java Objects),它们更像是在漂浮。再说一次,我对这些不是很熟悉,我可以尝试回答一些问题,但我希望这能让你到达你需要去的地方。

Happy coding! Leave a comment if you have any questions.

快乐编码!如果您有任何问题,请发表评论。

回答by haley

Check out the Gson API and some examples. I've put the links below!

查看 Gson API 和一些示例。我已经把链接放在下面了!

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)