[
{
"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
答案 0 :(得分:4)
我试图解析那个JSON字符串,我有一个解决方案,请尝试这个:
String parse = "[{\"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\"}]}]";
try {
JSONArray menuObject = new JSONArray(parse);
for(int i=0;i<menuObject.length();i++){
String hotel = menuObject.getJSONObject(i).getString("hotelid").toString();
System.out.println("hotel="+hotel);
JSONArray menuObject1 = new JSONArray(hotel);
for(int j=0; j<menuObject1.length();j++){
String hotelid = menuObject1.getJSONObject(j).getString("hotelid").toString();
System.out.println("hotelid=="+hotelid);
String name = menuObject1.getJSONObject(j).getString("name").toString();
System.out.println("name=="+name);
String code = menuObject1.getJSONObject(j).getString("code").toString();
System.out.println("code=="+code);
String price = menuObject1.getJSONObject(j).getString("price").toString();
System.out.println("price=="+price);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:3)
这个问题被问过这么多次。
使用com.google.gson
包。它与org.json
包(json.jar)的99%相似。
下次你应该谷歌。
答案 2 :(得分:3)
以下是使用Jackson作为Java-to / from-JSON库的示例。 Jackson是快速,功能最丰富的Java / JSON API之一。
我猜测了实际目标JSON结构是什么。
import java.util.List;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.map.ObjectMapper;
public class Foo
{
public static void main(String[] args) throws Exception
{
// input:
// [
// {
// "hotel": [
// {"id":"52472","name":"africa","hotel":"asd"},
// {"id":"52471","name":"europe","hotel":"asd2"},
// {"id":"52470","name":"europe","hotel":"asd3"}
// ]
// }
// ]
String input = "[{\"hotel\":[{\"id\":\"52472\",\"name\":\"africa\",\"hotel\":\"asd\"},{\"id\":\"52471\",\"name\":\"europe\",\"hotel\":\"asd2\"},{\"id\":\"52470\",\"name\":\"europe\",\"hotel\":\"asd3\"}]}]";
ObjectMapper mapper = new ObjectMapper();
// configure Jackson to access non-public fields
mapper.setVisibilityChecker(mapper.getVisibilityChecker().withFieldVisibility(Visibility.ANY));
Result[] results = mapper.readValue(input, Result[].class);
Result result = results[0];
System.out.println(result);
}
}
class Result
{
List<Hotel> hotel; // name matches name in JSON
@Override
public String toString() {return hotel.toString();}
}
class Hotel
{
String id; // name matches name in JSON
String name; // name matches name in JSON
String hotel; // name matches name in JSON
@Override
public String toString()
{
return String.format("Hotel:{id=%s, name=%s, hotel=%s}", id, name, hotel);
}
}
答案 3 :(得分:3)
使课程结果和酒店静止。解析器无法创建非静态的内部类的实例
答案 4 :(得分:2)
此JSON无效。你错过了关闭]支架。您可以查看您的JSON here