开发者

How do I parse through data to create an object in java?

开发者 https://www.devze.com 2023-02-27 01:10 出处:网络
I am looking to find out how to parse through data that was received from a JSON site. Here is an example of the data.

I am looking to find out how to parse through data that was received from a JSON site. Here is an example of the data.

   { "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 }}

I want to make a ICAO object with all of this data and fill in the attributes with the above content.

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 da开发者_如何学运维te;
    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;
}


I've had good experiences with Google Gson.

If your ICAO class matches your JSON data, then conversion should be as simple as calling

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

Read the user guide for finer details.


You could use a combination of JAXB and JSON for this use case:

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;

}

Demo

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

For more information:

  • 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


You can try to use JSONTokener from JSON-Java


Or you can use Jackson. It will do an object mapping to bean-pattern classes for you.


I have had good luck using the JSON classes found here:

http://json.org/java/

You would essentially create a JSONObject instance like this:

JSONObject jsonObject = new JSONObject(data);

Where data is a string representation of your JSON data. From there you can use the methods:

get(String) - to get a particular property.

keys() - to get an enumeration of the translated keys.

What I've done in the past is to create a constructor in your custom object that receives JSON format data and uses this method to parse the expected fields into your instance variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消