Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

Android JSON parse this, how?

 [
    {
        "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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have tried to parse that JSON string and I got one solution please try this also:

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();
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...