15

I need to parse a JSON file which 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 don't get it how I can do it from my file.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ProblemAnswerQue
  • 535
  • 1
  • 3
  • 18

3 Answers3

29

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);
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Yazan
  • 6,074
  • 1
  • 19
  • 33
2

Check out the Gson API and some examples. 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)

haley
  • 1,165
  • 7
  • 13
  • now I have this: private static final String filePath = ".\\data.json"; BufferedReader br; Gson gson = new Gson(); Click click = gson.fromJson(br, Click.class); System.out.println(click); but it still says my json file has a wrong startingobject and ending object – ProblemAnswerQue Dec 23 '14 at 21:46
  • I'm not sure how BufferedReader works in that method. I didn't know you could do that. That might be why you're getting the error. – haley Dec 23 '14 at 22:03
  • Are you stuck on getting the json into a string? – haley Dec 23 '14 at 22:05
0

Another solid option is Jackson it seems to have a pretty solid tutorial. 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

EDIT:

If you're pretty set on using Gson, perhaps looking at other similar answers will help. This question is 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.

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

Community
  • 1
  • 1
Matt
  • 5,404
  • 3
  • 27
  • 39
  • yeah I know Jackson exists but gson seems more userfriendly and faster in it's use but I just don't get it, User.class will be Click.class, that i already knew same in gson for as far as I figured out – ProblemAnswerQue Dec 23 '14 at 21:24
  • for Jackson to work you need to also have an empty constructor for your class – Kostas Kryptos Dec 23 '14 at 21:24