如何解析数据以在java中创建对象?
我希望了解如何解析从JSON站点接收的数据。下面是一个数据示例如何解析数据以在java中创建对象?,java,json,parsing,object,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", "seaLevelP
{ "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;
}
你可以试着使用from我在这方面有很好的经验 如果您的ICAO类与JSON数据匹配,那么转换应该像调用
Gson gson = new Gson();
gson.fromJson(JSONstring, ICAO.class);
有关详细信息,请阅读。或者您可以使用。它将为您执行到bean模式类的对象映射。您可以将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);
}
}
有关更多信息:
JSONObject jsonObject = new JSONObject(data);
其中数据是JSON数据的字符串表示形式。从那里,您可以使用以下方法:
获取(字符串)-获取特定的
财产
keys()-获取
翻译的钥匙
我过去所做的是在自定义对象中创建一个构造函数,该构造函数接收JSON格式的数据,并使用此方法将预期字段解析为实例变量