2011-11-25 7 views
-2

Gson을 사용하여이 Json을 구문 분석하고 Placetype _Name 및 장소 세부 정보를 표시하는 방법은 무엇입니까?Gson을 사용하여이 Json을 구문 분석하는 방법

나는이 json이 있으며, Placetype _Name 및 장소 세부 정보를 분석하고 표시하는 데 도움이 필요합니다.

{ 
    "PlacesList": { 
"PlaceType": [ 
    { 
    "-Name": "Airport", 
    "Places": { 
     "Place": [ 
     { 
      "name": "Juhu Aerodrome", 
      "latitude": "19.09778", 
      "longitude": "72.83083", 
      "description": "Juhu Aerodrome is an airport that serves the metropolitan" 
     }, 
     { 
      "name": "Chhatrapati Shivaji International Airport", 
      "latitude": "19.09353", 
      "longitude": "72.85489", 
      "description": "Chhatrapati Shivaji International Airport " 
     } 
     ] 
    } 
    }, 
    { 
    "-Name": "Mall", 
    "Places": { 
     "Place": [ 
     { 
      "name": "Infinity", 
      "latitude": "19.14030", 
      "longitude": "72.83180", 
      "description": "This Mall is one of the best places for all types of brand" 
     }, 
     { 
      "name": "Heera Panna", 
      "latitude": "18.98283", 
      "longitude": "72.80897", 
      "description": "The Heera Panna Shopping Center is one of the most popular" 
     } 
     ] 
    } 
    } 
] 
    } 
} 

답변

5

로 시작하는 JSON 요소 이름 처리하기 쉬운 솔루션 "-"Java 식별자의 이름을 시작하는 데 사용할 수없는 문자의 @SerializedName 주석을 사용하는 것이다.

import java.io.FileReader; 
import java.util.List; 

import com.google.gson.Gson; 
import com.google.gson.annotations.SerializedName; 

public class GsonFoo 
{ 
    public static void main(String[] args) throws Exception 
    { 
    Response response = new Gson().fromJson(new FileReader("input.json"), Response.class); 
    System.out.println(response.PlacesList.PlaceType.get(0).Name); 
    System.out.println(response.PlacesList.PlaceType.get(0).Places.Place.get(0).name); 
    System.out.println(response.PlacesList.PlaceType.get(0).Places.Place.get(0).description); 
    } 
} 

class Response 
{ 
    PlacesList PlacesList; 
} 

class PlacesList 
{ 
    List<PlaceType> PlaceType; 
} 

class PlaceType 
{ 
    @SerializedName("-Name") 
    String Name; 
    Places Places; 
} 

class Places 
{ 
    List<Place> Place; 
} 

class Place 
{ 
    String name; 
    String latitude; 
    String longitude; 
    String description; 
} 

출력 :

Airport 
Juhu Aerodrome 
Juhu Aerodrome is an airport that serves the metropolitan 
관련 문제