[
{
"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"
}
...
I have this JSON, how can I parse it in android? I tried it, but i only get errors.
code:
@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+"");
}
}
ERROR/err(23124): org.codehaus.jackson.map.JsonMappingException: Can
not deserialize Class com.android.asd.asdStart$Result (of type
non-static member class) as a Bean
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…