简体   繁体   English

如何从 Java 中的 API 响应中提取信息

[英]How to extract information from API Response in Java

I´m pretty new to Java and I stumbled on a following problem.我对 Java 很陌生,我偶然发现了以下问题。 I would like to extract information from an API response as following:我想从 API 响应中提取信息,如下所示:

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

What I would need to extract is the address1 information.我需要提取的是 address1 信息。 Anyone any ideas?有人有什么想法吗?

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

You'll want to create an object that models the JSON you are ingesting.您需要创建一个 object 来模拟您正在摄取的 JSON。 You'll then use Jackson to transform that JSON response into the java object, where you can work with it normally.然后,您将使用 Jackson 将 JSON 响应转换为 java ZA8CFDE6331BD59EB26ACZF8,您可以正常使用它。

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

Example: https://www.tutorialspoint.com/jackson/jackson_object_serialization.htm示例: 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+ " ]";
   }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM