如何解析数据以在Java中创建对象?

| 我正在寻找找出如何解析从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;
}
已邀请:
我在Google Gson方面有很好的经验。 如果您的ICAO类与您的JSON数据匹配,那么转换应该像调用一样简单
Gson gson = new Gson();
gson.fromJson(JSONstring, ICAO.class);
阅读用户指南以获取更多详细信息。
您可以在此用例中结合使用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);
    }
}
欲获得更多信息: http://bdoughan.blogspot.com/2011/04/jaxb-and-json-via-jettison.html http://bdoughan.blogspot.com/2011/04/jaxb-and-json-via-jettison-namespace.html
您可以尝试使用JSON-Java中的JSONTokener
或者您可以使用Jackson。它将为您执行一个对象映射到Bean模式类。
我使用这里找到的JSON类祝您好运: http://json.org/java/ 实际上,您将创建一个JSONObject实例,如下所示:
JSONObject jsonObject = new JSONObject(data);
其中data是JSON数据的字符串表示形式。从那里可以使用以下方法: get(String)-获得一个特定的 属性。 keys()-获得一个枚举 翻译的密钥。 我过去所做的就是在您的自定义对象中创建一个构造函数,该构造函数接收JSON格式的数据,并使用此方法将期望的字段解析为实例变量。

要回复问题请先登录注册