这里的JsonObject是org.json.JSONObject;
JSONObject jsonObject = new JSONObject(jsonstring);
我在as中写了带有主函数的测试类,然后使用JsonObject,然后一直给我报下面这个错
Exception in thread "main" java.lang.RuntimeException: Stub!
at org.json.JSONObject.(JSONObject.java:7)
at com.example.administrator.weatherforecast.Test.main(Test.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
我还以为是json字符串格式不对了,然后各种尝试还是不行,最后百度了下,才知道,这是 Java工程中调用Android库出现“Stub!”错误 .
出现这种错误的原因: Android工程和Java工程还有一定的差异,不能混用他们的库,和函数入口方法。
将上面的代码,移植到在Android工程可以正确执行!
public class SecondActivity extends Activity {
private TextView mSecond_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
init();
}
private void init() {
findViewById(R.id.second_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
parseData();
}
});
}
/**
* 解析json数据
*/
private void parseData() {
String jsonstring ="{\"coord\":{\"lon\":116.4,\"lat\":39.91},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"clear sky\",\"icon\":\"01d\"}],\"base\":\"stations\",\"main\":{\"temp\":291.15,\"pressure\":1009,\"humidity\":82,\"temp_min\":291.15,\"temp_max\":291.15},\"visibility\":10000,\"wind\":{\"speed\":2,\"deg\":120},\"clouds\":{\"all\":0},\"dt\":1496739600,\"sys\":{\"type\":1,\"id\":7405,\"message\":0.0246,\"country\":\"CN\",\"sunrise\":1496695574,\"sunset\":1496749219},\"id\":1816670,\"name\":\"Beijing\",\"cod\":200}";
try {
/**
* {
* "coord":{"lon":116.4,"lat":39.91},
* "weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],
* "base":"stations",
* "main":{"temp":291.15,"pressure":1009,"humidity":82,"temp_min":291.15,"temp_max":291.15},
* "visibility":10000,
* "wind":{"speed":2,"deg":120},
* "clouds":{"all":0},
* "dt":1496739600,
* "sys":{"type":1,"id":7405,"message":0.0246,"country":"CN","sunrise":1496695574,"sunset":1496749219},
* "id":1816670,
* "name":"Beijing",
* "cod":200
* }
*/
JSONObject jsonObject = new JSONObject(jsonstring);
JSONObject coord = jsonObject.getJSONObject("coord");
double lat = coord.getDouble("lat");
System.out.println("lat = "+lat);
JSONArray weather = jsonObject.getJSONArray("weather");
String icon = weather.getJSONObject(0).getString("icon");
System.out.println("icon = "+icon);
String base = jsonObject.getString("base");
System.out.println("base = "+base);
/**
* 06-07 10:18:45.728 6543-6543/com.example.administrator.weatherforecast I/System.out: lat = 39.91
06-07 10:18:45.728 6543-6543/com.example.administrator.weatherforecast I/System.out: icon = 01d
06-07 10:18:45.728 6543-6543/com.example.administrator.weatherforecast I/System.out: base = stations
*/
} catch (JSONException e) {
System.out.println("解析出错");
}
}
}
总结android中的JsonObject使用十分简单,就一句话通过键找值,当然要选对对应的方法,值对应的是Object就是 getJSONObject,是数组就是getJSONArray,是基本数据类型就是对应的基本数据类型的方法.
虽然说现在都有成熟的解析json的框架可以用,但是使用JsonObject获取json字符串的单个字段或是少数字段还是很好了,必须完全解析,提高性能,我猜的,我也不确定.