2017-12-10 3 views
0

StringBuffer를 구문 분석하여 문자열 목록에 넣은 다음 listview를 사용하여 모든 레스토랑 이름을 나열하려고했지만 StringBuffer를 변환하는 데 문제가 있습니다. JsonObject로, 왜냐하면 내가 getAsJsonObject()를 시도 할 때마다; 함수 내 애플 리케이션 충돌 누구나 내가 JsonObject에 StringBuffer를 캐스팅 할 수없는 이유와 JSON 객체로 변환 할 버퍼를 제대로 얻는 방법을 알아낼 수 있습니다. 도움을 주시면 감사하겠습니다. 당신이 restaurantsJSONArray에 NPE를 얻을 경우JsonObject를 문자열 목록으로 변환

{ 
    "results_found": 10573, 
    "results_start": 0, 
    "results_shown": 20, 
    "restaurants": [ 
    { 
     "restaurant": { 
     "R": { 
      "res_id": 16795271 
     }, 
     "apikey": "fa221c30c201daf8380ac435cedfebe9", 
     "id": "16795271", 
     "name": "Craigie On Main", //This is what I want to sort it by 
     "url": "https://www.zomato.com/boston/craigie-on-main-cambridge?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "location": { 
      "address": "853 Main St, Cambridge 02139", 
      "locality": "Central Square", 
      "city": "Boston", 
      "city_id": 289, 
      "latitude": "42.3634820000", 
      "longitude": "-71.0985660000", 
      "zipcode": "02139", 
      "country_id": 216, 
      "locality_verbose": "Central Square, Boston" 
     }, 
     "switch_to_order_menu": 0, 
     "cuisines": "European, French", 
     "average_cost_for_two": 275, 
     "price_range": 4, 
     "currency": "$", 
     "offers": [], 
     "thumb": "https://b.zmtcdn.com/data/res_imagery/16795271_CHAIN_bd36a0893cde3f70ab4a67f1e086d5f5.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A", 
     "user_rating": { 
      "aggregate_rating": "4.2", 
      "rating_text": "Very Good", 
      "rating_color": "5BA829", 
      "votes": "342" 
     }, 
     "photos_url": "https://www.zomato.com/boston/craigie-on-main-cambridge/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop", 
     "menu_url": "https://www.zomato.com/boston/craigie-on-main-cambridge/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop", 
     "featured_image": "https://b.zmtcdn.com/data/res_imagery/16795271_CHAIN_bd36a0893cde3f70ab4a67f1e086d5f5.jpg", 
     "has_online_delivery": 0, 
     "is_delivering_now": 0, 
     "deeplink": "zomato://restaurant/16795271", 
     "has_table_booking": 0, 
     "events_url": "https://www.zomato.com/boston/craigie-on-main-cambridge/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "establishment_types": [] 
     } 
    }, 
    { 
     "restaurant": { 
     "R": { 
      "res_id": 16798941 
     }, 
     "apikey": "fa221c30c201daf8380ac435cedfebe9", 
     "id": "16798941", 
     "name": "O Ya", 
     "url": "https://www.zomato.com/boston/o-ya-boston?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "location": { 
      "address": "9 East St, Boston 02111", 
      "locality": "Leather District", 
      "city": "Boston", 
      "city_id": 289, 
      "latitude": "42.3513170000", 
      "longitude": "-71.0570370000", 
      "zipcode": "02111", 
      "country_id": 216, 
      "locality_verbose": "Leather District, Boston" 
     }, 
     "switch_to_order_menu": 0, 
     "cuisines": "Japanese, Sushi", 
     "average_cost_for_two": 275, 
     "price_range": 4, 
     "currency": "$", 
     "offers": [], 
     "thumb": "https://b.zmtcdn.com/data/res_imagery/16798941_CHAIN_d58be7a0f56ad4fbad498ba65b746a67.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A", 
     "user_rating": { 
      "aggregate_rating": "4.3", 
      "rating_text": "Very Good", 
      "rating_color": "5BA829", 
      "votes": "192" 
     }, 
     "photos_url": "https://www.zomato.com/boston/o-ya-boston/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop", 
     "menu_url": "https://www.zomato.com/boston/o-ya-boston/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop", 
     "featured_image": "https://b.zmtcdn.com/data/res_imagery/16798941_CHAIN_d58be7a0f56ad4fbad498ba65b746a67.jpg?output-format=webp", 
     "has_online_delivery": 0, 
     "is_delivering_now": 0, 
     "deeplink": "zomato://restaurant/16798941", 
     "has_table_booking": 0, 
     "events_url": "https://www.zomato.com/boston/o-ya-boston/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "establishment_types": [] 
     } 
    }, 
    { 
     "restaurant": { 
     "R": { 
      "res_id": 16798079 
     }, 
     "apikey": "fa221c30c201daf8380ac435cedfebe9", 
     "id": "16798079", 
     "name": "Solea Restaurant and Tapas Bar", 
     "url": "https://www.zomato.com/boston/solea-restaurant-and-tapas-bar-waltham?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "location": { 
      "address": "388 Moody St, Waltham 02453", 
      "locality": "Waltham", 
      "city": "Boston", 
      "city_id": 289, 
      "latitude": "42.3695490000", 
      "longitude": "-71.2370130000", 
      "zipcode": "02453", 
      "country_id": 216, 
      "locality_verbose": "Waltham, Boston" 
     }, 
     "switch_to_order_menu": 0, 
     "cuisines": "Spanish, Tapas", 
     "average_cost_for_two": 275, 
     "price_range": 4, 
     "currency": "$", 
     "offers": [], 
     "thumb": "", 
     "user_rating": { 
      "aggregate_rating": "4.2", 
      "rating_text": "Very Good", 
      "rating_color": "5BA829", 
      "votes": "303" 
     }, 
     "photos_url": "https://www.zomato.com/boston/solea-restaurant-and-tapas-bar-waltham/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop", 
     "menu_url": "https://www.zomato.com/boston/solea-restaurant-and-tapas-bar-waltham/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop", 
     "featured_image": "", 
     "has_online_delivery": 0, 
     "is_delivering_now": 0, 
     "deeplink": "zomato://restaurant/16798079", 
     "has_table_booking": 0, 
     "events_url": "https://www.zomato.com/boston/solea-restaurant-and-tapas-bar-waltham/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "establishment_types": [] 
     } 
    }, 
"restaurant": { 
     "R": { 
      "res_id": 16796139 
     }, 
     "apikey": "fa221c30c201daf8380ac435cedfebe9", 
     "id": "16796139", 
     "name": "Harvest", 
     "url": "https://www.zomato.com/boston/harvest-cambridge?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "location": { 
      "address": "44 Brattle St, Cambridge 02138", 
      "locality": "Harvard Square", 
      "city": "Boston", 
      "city_id": 289, 
      "latitude": "42.3742680000", 
      "longitude": "-71.1219960000", 
      "zipcode": "02138", 
      "country_id": 216, 
      "locality_verbose": "Harvard Square, Boston" 
     }, 
     "switch_to_order_menu": 0, 
     "cuisines": "American, Breakfast", 
     "average_cost_for_two": 275, 
     "price_range": 4, 
     "currency": "$", 
     "offers": [], 
     "thumb": "https://b.zmtcdn.com/data/res_imagery/16796139_RESTAURANT_4f9960714d6bd153a4571dd062c4ff45.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A", 
     "user_rating": { 
      "aggregate_rating": "3.9", 
      "rating_text": "Good", 
      "rating_color": "9ACD32", 
      "votes": "190" 
     }, 
     "photos_url": "https://www.zomato.com/boston/harvest-cambridge/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop", 
     "menu_url": "https://www.zomato.com/boston/harvest-cambridge/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop", 
     "featured_image": "https://b.zmtcdn.com/data/res_imagery/16796139_RESTAURANT_4f9960714d6bd153a4571dd062c4ff45.jpg", 
     "has_online_delivery": 0, 
     "is_delivering_now": 0, 
     "deeplink": "zomato://restaurant/16796139", 
     "has_table_booking": 0, 
     "events_url": "https://www.zomato.com/boston/harvest-cambridge/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "establishment_types": [] 
     } 
    }, 
+0

방금 ​​귀하의 코드를 시도했지만 그것은 나를 위해 충돌하지 않습니다. JsonArray restaurantsJSONArray가 성공적으로 채워집니다. 나는 유효한 json 문자열이 아니므로 StringBuffer를 변경했습니다. http://json.parser.online.fr/ –

+0

과 같은 웹 사이트에서 json 문자열 유효성을 확인하십시오. 오류 : null 객체 참조에 가상 메서드를 호출하려고 시도했습니다. – Steve

+0

이 오류는 JsonElements가 시도 할 때 발생합니다. restaurantJSONElement = ((JsonObject) restaurantsJSONArray.get (i)). get ("restaurant"); 왜 아무것도 null로 설정하지 않았기 때문에 내가 이걸 얻고 있는지 모르겠다. – Steve

답변

0

내가 그것을 잘 보이는 개체에 대한 일부 요소를 보인다 제대로 당신이 JsonArray에 JsonObj을 변환하지 않은 가정 :

String restaurantName; 
    JsonElement restaurantJSONElement; 
    JsonPrimitive restaurantJSONPrimitive; 


    JsonParser parser = new JsonParser(); 
    // buffer is the StringBuffer read 
    JsonElement parentJSONElement = parser.parse(String.valueOf(buffer)); 
    JsonObject parentJSONObject = parentJSONElement.getAsJsonObject(); 
//this is where is crashes I assume its because it cant convert the JsonElement to a JsonObject 
    JsonArray restaurantsJSONArray = (JsonArray) parentJSONObject.get("restaurants"); 
    List<String> restaurantnamelist1 = new ArrayList<String>(); 

    { 
     for (int i = 0; i < 1; i++) { 
      //get the element 
      restaurantJSONElement = ((JsonObject) restaurantsJSONArray.get(i)).get("restaurant"); 
      //get the restaurant name 
      restaurantJSONPrimitive = ((JsonObject) restaurantJSONElement.getAsJsonObject()).getAsJsonPrimitive("name"); 
      restaurantName = restaurantJSONPrimitive.getAsString(); 
      restaurantnamelist1.add(restaurantName); 
     } 
    } 

은이는 StringBuffer에 포함 된 내용입니다

JsonArray restaurantsJSONArray = parentJSONObject.getAsJsonArray("restaurants"); 
0

그렇게 나는 아래의 문자열 버퍼를 사용하는 시도가 제대로 작동 :

{ 
    "results_found": 10573, 
    "results_start": 0, 
    "results_shown": 20, 
    "restaurants": [ 
    { 
     "restaurant": { 
     "R": { 
      "res_id": 16795271 
     }, 
     "apikey": "fa221c30c201daf8380ac435cedfebe9", 
     "id": "16795271", 
     "name": "Craigie On Main", 
     "url": "https://www.zomato.com/boston/craigie-on-main-cambridge?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "location": { 
      "address": "853 Main St, Cambridge 02139", 
      "locality": "Central Square", 
      "city": "Boston", 
      "city_id": 289, 
      "latitude": "42.3634820000", 
      "longitude": "-71.0985660000", 
      "zipcode": "02139", 
      "country_id": 216, 
      "locality_verbose": "Central Square, Boston" 
     }, 
     "switch_to_order_menu": 0, 
     "cuisines": "European, French", 
     "average_cost_for_two": 275, 
     "price_range": 4, 
     "currency": "$", 
     "offers": [], 
     "thumb": "https://b.zmtcdn.com/data/res_imagery/16795271_CHAIN_bd36a0893cde3f70ab4a67f1e086d5f5.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A", 
     "user_rating": { 
      "aggregate_rating": "4.2", 
      "rating_text": "Very Good", 
      "rating_color": "5BA829", 
      "votes": "342" 
     }, 
     "photos_url": "https://www.zomato.com/boston/craigie-on-main-cambridge/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop", 
     "menu_url": "https://www.zomato.com/boston/craigie-on-main-cambridge/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop", 
     "featured_image": "https://b.zmtcdn.com/data/res_imagery/16795271_CHAIN_bd36a0893cde3f70ab4a67f1e086d5f5.jpg", 
     "has_online_delivery": 0, 
     "is_delivering_now": 0, 
     "deeplink": "zomato://restaurant/16795271", 
     "has_table_booking": 0, 
     "events_url": "https://www.zomato.com/boston/craigie-on-main-cambridge/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "establishment_types": [] 
     } 
    }, 
    { 
     "restaurant": { 
     "R": { 
      "res_id": 16798941 
     }, 
     "apikey": "fa221c30c201daf8380ac435cedfebe9", 
     "id": "16798941", 
     "name": "O Ya", 
     "url": "https://www.zomato.com/boston/o-ya-boston?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "location": { 
      "address": "9 East St, Boston 02111", 
      "locality": "Leather District", 
      "city": "Boston", 
      "city_id": 289, 
      "latitude": "42.3513170000", 
      "longitude": "-71.0570370000", 
      "zipcode": "02111", 
      "country_id": 216, 
      "locality_verbose": "Leather District, Boston" 
     }, 
     "switch_to_order_menu": 0, 
     "cuisines": "Japanese, Sushi", 
     "average_cost_for_two": 275, 
     "price_range": 4, 
     "currency": "$", 
     "offers": [], 
     "thumb": "https://b.zmtcdn.com/data/res_imagery/16798941_CHAIN_d58be7a0f56ad4fbad498ba65b746a67.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A", 
     "user_rating": { 
      "aggregate_rating": "4.3", 
      "rating_text": "Very Good", 
      "rating_color": "5BA829", 
      "votes": "192" 
     }, 
     "photos_url": "https://www.zomato.com/boston/o-ya-boston/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop", 
     "menu_url": "https://www.zomato.com/boston/o-ya-boston/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop", 
     "featured_image": "https://b.zmtcdn.com/data/res_imagery/16798941_CHAIN_d58be7a0f56ad4fbad498ba65b746a67.jpg?output-format=webp", 
     "has_online_delivery": 0, 
     "is_delivering_now": 0, 
     "deeplink": "zomato://restaurant/16798941", 
     "has_table_booking": 0, 
     "events_url": "https://www.zomato.com/boston/o-ya-boston/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1", 
     "establishment_types": [] 
     } 
    }]} 
관련 문제