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

时间:2011-04-18 19:50:28

标签: java json parsing object

我希望了解如何解析从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;
}

5 个答案:

答案 0 :(得分:2)

我对Google Gson有很好的体验。

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

一样简单
Gson gson = new Gson();
gson.fromJson(JSONstring, ICAO.class);

阅读user guide以获取更详细的信息。

答案 1 :(得分:2)

您可以将JAXB和JSON的组合用于此用例:

<强> ICAO

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);
    }
}

有关详情:

答案 2 :(得分:1)

您可以尝试使用JSONTokener

中的JSON-Java

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

http://json.org/java/

您实际上将创建一个这样的JSONObject实例:

JSONObject jsonObject = new JSONObject(data);

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

  

get(String) - 得到一个特定的   属性。

     

keys() - 获取枚举   翻译的密钥。

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