【问题标题】:How do I parse through data to create an object in java?java - 如何解析数据以在java中创建对象?
【发布时间】:2011-04-18 19:50:28
【问题描述】:

我正在寻找如何解析从 JSON 站点接收到的数据。以下是数据示例。

   { "weatherObservation":{ 
  "clouds": "n/a",
  "weatherCondition": "n/a",
  "observation": "KIAD 181852Z 18005KT 10SM CLR 21/06 A2992 RMK AO2 SLP132 T02110056",
  "windDirection": 180,
  "ICAO": "KIAD",
  "seaLevelPressure": 1013.2,
  "elevation": 93,
  "countryCode": "US",
  "lng": -77.45,
  "temperature": "21.1",
  "dewPoint": "5.6",
  "windSpeed": "05",
  "humidity": 36,
  "stationName": "Washington DC, Washington-Dulles International Airport",
  "datetime": "2011-04-18 18:52:00",
  "lat": 38.93333333333333 }}

我想用所有这些数据制作一个ICAO对象,并用上述内容填写属性。

public class ICAO {

    String clouds;
    String weatherCondition;
    String observation;
    int windDirection;
    String ICAOid;
    int seaLevelPressure;
    int elevation;
    String countryCode;
    double lng;
    double temperature;
    double dewpoint;
    int windSpeed;
    int humidity;
    String stationName;
    String date;
    double lat;


public ICAO(String _clouds,String _weatherCondition,String _observation,int _windDirection,String _ICAOid,int _seaLevelPressure,int _elevation, String _countryCode, 
    double _lng, double _temperature, double _dewpoint, int _windSpeed, int _humidity, String _stationName, String _date, double _lat)
{
    clouds = _clouds;
    weatherCondition = _weatherCondition;
    observation = _observation;
    windDirection = _windDirection;
    ICAOid = _ICAOid;
    seaLevelPressure = _seaLevelPressure;
    elevation = _elevation;
    countryCode = _countryCode;
    lng = _lng;
    temperature = _temperature;
    dewpoint = _dewpoint;
    windSpeed = _windSpeed;
    humidity = _humidity;
    stationName = _stationName;
    date = _date;
    lat = _lat;
}

【问题讨论】:

    标签: java json parsing object


    【解决方案1】:

    我对@9​​87654321@ 有很好的体验。

    如果您的 ICAO 类与您的 JSON 数据匹配,那么转换应该像调用一样简单

    Gson gson = new Gson();
    gson.fromJson(JSONstring, ICAO.class);
    

    阅读user guide了解更多详情。

    【讨论】:

      【解决方案2】:

      对于这个用例,您可以结合使用 JAXB 和 JSON:

      国际民航组织

      import javax.xml.bind.annotation.XmlAccessorType;
      import javax.xml.bind.annotation.XmlAccessType;
      import javax.xml.bind.annotation.XmlElement;
      import javax.xml.bind.annotation.XmlRootElement;
      
      @XmlRootElement(name="weatherObservation")
      @XmlAccessorType(XmlAccessType.FIELD)
      public class ICAO {
      
          String clouds;
          String weatherCondition;
          String observation;
          int windDirection;
          @XmlElement(name="ICAO") String ICAOid;
          int seaLevelPressure;
          int elevation;
          String countryCode;
          double lng;
          double temperature;
          double dewpoint;
          int windSpeed;
          int humidity;
          String stationName;
          @XmlElement(name="datetime") String date;
          double lat;
      
      }
      

      演示

      import javax.xml.bind.JAXBContext;
      import javax.xml.bind.Marshaller;
      import javax.xml.bind.Unmarshaller;
      import javax.xml.stream.XMLStreamReader;
      
      import org.codehaus.jettison.json.JSONObject;
      import org.codehaus.jettison.mapped.Configuration;
      import org.codehaus.jettison.mapped.MappedNamespaceConvention;
      import org.codehaus.jettison.mapped.MappedXMLStreamReader;
      
      public class Demo {
      
          public static void main(String[] args) throws Exception {
              JAXBContext jc = JAXBContext.newInstance(ICAO.class);
      
              JSONObject obj = new JSONObject("{\"weatherObservation\":{\"clouds\": \"n/a\",\"weatherCondition\": \"n/a\",\"observation\": \"KIAD 181852Z 18005KT 10SM CLR 21/06 A2992 RMK AO2 SLP132 T02110056\",\"windDirection\": 180,\"ICAO\": \"KIAD\",\"seaLevelPressure\": 1013.2,\"elevation\": 93,\"countryCode\": \"US\",\"lng\": -77.45,\"temperature\": \"21.1\",\"dewPoint\": \"5.6\",\"windSpeed\": \"05\",\"humidity\": 36,\"stationName\": \"Washington DC, Washington-Dulles International Airport\",\"datetime\": \"2011-04-18 18:52:00\",\"lat\": 38.93333333333333 }}");
              Configuration config = new Configuration();
              MappedNamespaceConvention con = new MappedNamespaceConvention(config);
              XMLStreamReader xmlStreamReader = new MappedXMLStreamReader(obj, con);
      
              Unmarshaller unmarshaller = jc.createUnmarshaller();
              ICAO icao = (ICAO) unmarshaller.unmarshal(xmlStreamReader);
      
              Marshaller marshaller = jc.createMarshaller();
              marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
              marshaller.marshal(icao, System.out);
          }
      }
      

      更多信息:

      【讨论】:

        【解决方案3】:

        你可以尝试从JSON-Java使用JSONTokener

        【讨论】:

          【解决方案4】:

          或者您可以使用Jackson。它会为你做一个到 bean-pattern 类的对象映射。

          【讨论】:

            【解决方案5】:

            我很幸运使用这里找到的 JSON 类:

            http://json.org/java/

            您实际上会像这样创建一个 JSONObject 实例:

            JSONObject jsonObject = new JSONObject(data);
            

            其中 data 是 JSON 数据的字符串表示形式。从那里您可以使用以下方法:

            get(String) - 获取特定的 财产。

            keys() - 获取 翻译键。

            我过去所做的是在您的自定义对象中创建一个构造函数,该构造函数接收 JSON 格式的数据并使用此方法将预期的字段解析为您的实例变量。

            【讨论】:

              猜你喜欢
              • 2014-12-25
              • 1970-01-01
              • 2013-04-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多