1

I have a short question concerning Java. I am trying to find/get a function to do the following in Java which I did in PHP. The PHP code looks like this:

PHP:    
$var = file_get_contents("https://api.example.com/json); //get json file
$var = json_decode($var, true); // decode json file to multidimensional array

I then can go on and read the values like this:

$firstname = $var["person data"]["firstname"]; //first container is "person data" which has a couple of values, too (like firstname)
$surname = $var["person data"]["surname"]; // and so on

.

This is what I got now (Java):

    URLConnection connection = new URL("https://example.com/json").openConnection();
    try(Scanner scanner = new Scanner(connection.getInputStream());){
        String response = scanner.useDelimiter("\\A").next();
        System.out.println(response);
    }

I get some kind of object in "response" but I am not able to convert it to an array like in the PHP code above. I tried a lot of other suggested solutions which I found on google, but I am not able to do this.

So, to sum it up. Basically, what I want to do is:

Pseudocode:
get json from link;
convert json to array; 
read array; // like: $var = result[0][5];

Can someone help me?

A.c
  • 21
  • 5
  • Check this question: https://stackoverflow.com/questions/1927885/decode-json-data-in-java – Matt McAlister Dec 02 '17 at 19:06
  • Thanks! I downloaded jackson.jar which seems to be good and fast and imported it to my project structure. What do I have to do now? I dont understand it and can't find a working solution. – A.c Dec 02 '17 at 19:56
  • Wait. I think I got it! – A.c Dec 02 '17 at 20:32

0 Answers0