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

lacas
  • 13,928
  • 30
  • 109
  • 183
  • 1
    "I tried it, but i only get errors." Then you should post code and ask for help with the errors. As it stands, this question falls into the category of "gimme teh codez." – Glendon Trullinger Jul 13 '11 at 06:39

5 Answers5

4

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();
            }
Basil
  • 2,163
  • 2
  • 16
  • 25
3

This question is asked so many times.

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

You should Google it next time.

Community
  • 1
  • 1
ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
  • how come accepted answer work? your code is not related to JSON. you are using some other 3rd party JSON library. -1 for bad question. – ahmet alp balkan Jul 13 '11 at 16:33
3

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);  
  }
}
Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
  • 07-13 10:06:33.008: 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 – lacas Jul 13 '11 at 08:07
  • It looks like you copied the code differently than I provided it. It looks like you made something an inner class of something else. The code I posted doesn't have any inner class definitions. If you're still stuck, post the entire code necessary to replicate the problem. – Programmer Bruce Jul 13 '11 at 09:34
  • in your case you should make classes Result and Hotel static, because you move them into Activity class – Henry Pootle Jul 13 '11 at 10:58
  • yeah u are right. new errors: 07-13 16:38:59.676: ERROR/err(504): org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "hotelid" (Class com.android.asd.asdStart$Result), not marked as ignorable – lacas Jul 13 '11 at 14:39
3

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

Henry Pootle
  • 1,196
  • 1
  • 11
  • 30
2

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

Henry Pootle
  • 1,196
  • 1
  • 11
  • 30