【发布时间】:2011-10-04 05:36:37
【问题描述】:
[
{
"hotelid": [
{
"hotelid": "1",
"name": "aaa",
"code": "111",
"price": "111"
},
{
"hotelid": "2",
"name": "bbb",
"code": "112",
"price": "211"
},
{
"hotelid": "4",
"name": "ccc",
"code": "42",
"price": "411"
}
...
我有这个 JSON,如何在 android 中解析它?我试过了,但我只得到错误。
代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mycontext=this;
examineJSONFile();
}
class Result
{
List<Hotel> hotel; // name matches name in JSON
@Override
public String toString() {return hotel.toString();}
}
class Hotel
{
String code; // name matches name in JSON
String name; // name matches name in JSON
String hotelid; // name matches name in JSON
@Override
public String toString()
{
return String.format("hotelid:{code=%s, name=%s, hotelid=%s}", code, name, hotelid);
}
}
void examineJSONFile() {
InputStream is = this.getResources().openRawResource(R.raw.promo);
String s;
try {
s = HttpConnect.streamToString(is);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));
Result[] results = mapper.readValue(s, Result[].class);
Result result = results[0];
Log.e("res", result.toString()+"");
} catch (Exception e) {
Log.e("err", e+"");
}
}
错误/错误(23124):org.codehaus.jackson.map.JsonMappingException:可以 不反序列化 com.android.asd.asdStart$Result 类(类型 非静态成员类)作为 Bean
【问题讨论】:
-
"我试过了,但我只得到错误。"然后你应该发布代码并寻求错误的帮助。就目前而言,这个问题属于“gimme teh codez”的范畴。