2

So I'm working on a windows app to practice my coding and expand my knowledge and I'm having a big issue working with the JSON from Riot Games. I'm not really sure the terminology to look up because I've never learned how to work with JSON so I've been making fair progress using google-fu and stack-overflow as references, along with various documentation.

I received all the following data using inputstream and outputstream. But it's all as an unformatted JSON, no indents and really hard to read. I assume I used the wrong tool for the task here and it just read characters and printed them.

My question is this, what syntax should I be looking at to import a JSON from a URL to a JSON file. Then what should I look at to convert or read that JSON as multiple different Java objects so that I may use the data in my code.

Using Gson is an option I've explored already but I'm not comfortable with their syntax, and I'd rather steer clear of more 3rd party dependencies if i can help it.

Any idea's or discussion is welcome, I'm a little over my head with the JSON here so any discussion i can learn from would help.

2 Answers2

2

Despite your aversion of third party libraries, I would strongly consider looking into the Jackson library for java, and utilizing its deserialization/serialization features.

Essentially, you create java objects that can be mapped from JSON. For example, for the JSON:

{
  "email": "email@gmail.com",
  "googleId": "43243243242432",
  "name": "some name"
}

Create a java class:

public class User {

    private String email;
    private String googleId;
    private String name;

    // Getters and Setters...
}

You can then map the JSON String to the object, using Jackson's object mapper, e.g.:

ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(myJsonString, User.class);

This allows you to easily access the fields:

String name = user.getName(); // some name
String email = user.getEmail(); // email@gmail.com

and so on.

Kevin Grant
  • 591
  • 6
  • 15
  • I'll definitely do some reading on this, my aversion is mostly just because I don't want to depend on something else being updated in the future. But it's also sometimes the right choice. Considering the number of classes and members, it looks like jackson library might be the best choice atm. – Nau Firefox May 26 '16 at 00:50
  • @NauFirefox It's definitely not a bad thing to be weary of third party libraries, it is good you are aware of this! One of the great things about Jackson is because it is so widely-used, there are tonnes of resources out there on it, so it makes it fairly easy to learn and use. – Kevin Grant May 26 '16 at 02:12
1

If you want to do this entirely in Java just skip to the bottom. I was thinking of using Java Script at first. But, realized you might not want to do that half way through.

To get a JSON file from a URL you will most likely need to use Java Script. And in that vain JQuery, a tool for Java Script, has a function for this. See .getJSON(). If you really don't want to use any third parties it can be done in native javascript see https://stackoverflow.com/a/2499647/1361042. However, using JQuery is probably much easier.

$.getJSON('url', function(data) {
    //data is the JSON
});

Then you write the data(a JSON object) to a JSON file. This file can then be used in your Java, where the JSON file will be imported and used as a JSON object that you can convert to Java objects.

JSON objects are basically Java Script objects with a little bit different formatting. Java objects are in a different format from Java Script objects. So you will need to do some conversion. This questions touches on that.

Convert a JSON string to object in Java ME?

Java

However, it might be easier for you do this entirely in Java, if that's your strong point. If you want to do it entirely in Java the top part is pretty irrelevant.

This is what I found on Java being used:

simplest way to read json from a URL in java

Community
  • 1
  • 1
Mopok Rawr
  • 106
  • 2