开发者

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


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


This question is asked so many times.

Use com.google.gson package. it is 99% similar to org.json package (json.jar).

  • How to Parse a JSON Object In Android
  • Parse JSON data into Android ListView
  • how to parse this json obj android
  • JSON and ANDROID
  • How can I parse this JSON in Android?

You should Google it next time.


Following is an example using Jackson as the Java-to/from-JSON library. Jackson is one of the fast, most feature-rich Java/JSON APIs available.

I made a guess at what the actual target JSON structure is.

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


Make class Result and Hotel static. Parser can't create instance of inner class which is not static


This JSON is invalid. You miss closing ] bracket. You can check your JSON here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜