-1

I´m pretty new to Java and I stumbled on a following problem. I would like to extract information from an API response as following:

{"data":{"52WeekChange":-0.23800159,"SandP52WeekChange":-0.0445475,"address1":"Salesforce Tower".....

What I would need to extract is the address1 information. Anyone any ideas?

  • Create a POJO class with same properties/variables and use com.fasterxml.jackson library to convert json response to your POJO class. OR you can use org.json library to simply convert JSON string to JSON object. – maddy23285 Aug 13 '22 at 11:37
  • 1
    The API response appears to be [JSON](https://en.wikipedia.org/wiki/JSON). There are several [Java libraries](https://javarevisited.blogspot.com/2016/09/top-5-json-library-in-java-JEE.html) for working with JSON. Select a library, learn how to use it and you should be able to discover the answer to your question yourself. – Abra Aug 13 '22 at 12:03
  • @maddy23285 please change your comment into answer so I can mark it. – AndyInDaHouse Aug 13 '22 at 12:54
  • You can research various SO questions about how to parse JSON using Java. Here is one: [How to parse JSON in Java](https://stackoverflow.com/q/2591098/12567365); here is another: [Parse Json String using jackson Parser](https://stackoverflow.com/q/50991217/12567365) and [many more](https://www.google.com/search?q=how+to+parse+json+using+java+site%253Astackoverflow.com). – andrewJames Aug 13 '22 at 13:17
  • Also, depending on how you are handling the API response, the tool you are using to do that may have built-in support for converting the response to a POJO without you needing to explicitly use a parser such as Jackson. – andrewJames Aug 13 '22 at 13:17

1 Answers1

0

Go read about the Jackson library: https://github.com/FasterXML/jackson

You'll want to create an object that models the JSON you are ingesting. You'll then use Jackson to transform that JSON response into the java object, where you can work with it normally.

Tutorial: https://www.tutorialspoint.com/jackson/index.htm

Example: https://www.tutorialspoint.com/jackson/jackson_object_serialization.htm

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonTester {
   public static void main(String args[]){
      JacksonTester tester = new JacksonTester();
      try {
         Student student = new Student();
         student.setAge(10);
         student.setName("Mahesh");
         tester.writeJSON(student);

         Student student1 = tester.readJSON();
         System.out.println(student1);

      } catch (JsonParseException e) {
         e.printStackTrace();
      } catch (JsonMappingException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   private void writeJSON(Student student) throws JsonGenerationException, JsonMappingException, IOException{
      ObjectMapper mapper = new ObjectMapper(); 
      mapper.writeValue(new File("student.json"), student);
   }

   private Student readJSON() throws JsonParseException, JsonMappingException, IOException{
      ObjectMapper mapper = new ObjectMapper();
      Student student = mapper.readValue(new File("student.json"), Student.class);
      return student;
   }
}

class Student {
   private String name;
   private int age;
   public Student(){}
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String toString(){
      return "Student [ name: "+name+", age: "+ age+ " ]";
   }    
}