[{"FOOD_ID":"1","FOOD_NAME":"Orange"},
{"FOOD_ID":"2","FOOD_NAME":"Pear"},
{"FOOD_ID":"3","FOOD_NAME":"Ham"},
{"FOOD_ID":"4","FOOD_NAME":"Melon"},
{"FOOD_ID":"5","FOOD_NAME":"WaterMelon"},
{"FOOD_ID":"6","FOOD_NAME":"Search"}]
{
"html_attributions" : [ "출처: \u003ca href="\"http://www.paginegialle.it/\"\u003ePagineGialle.it\u003c/a\u003e" ],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 45.7849210,
"lng" : 11.8724490
},
"viewport" : {
"northeast" : {
"lat" : 45.81619110,
"lng" : 11.89575470
},
"southwest" : {
"lat" : 45.75432980,
"lng" : 11.84532520
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "a454072e2ba71d4e2331e39099261f086cd11d1e",
"name" : "Fonte",
"reference" : "CnRtAAAAD65CDwuvnxxduBzQJEQH2QrZg-UEKO4AAFe4BMgcz_UtldLCZaPQYzGWG_lli7g6OEa--O5S99D-sSHpI-E6ogKnZOQJPVwcN1CuAm6WXu7-Pij8TeWLwnd5FsRQ8ktNqaUj9v9r1mlQWN4--p66-hIQUDbn8QMVhWqvAdd6mkAeQxoU16OqaLupg6NJH8mDQPQQ0AT0j8Q",
"types" : [ "locality", "political" ],
"vicinity" : "Fonte"
},
{
"geometry" : {
"location" : {
"lat" : 45.7870960,
"lng" : 11.8675870
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/civic_building-71.png",
"id" : "2bf5c15d370ed0bf231cb8d6f43d74c336d94a55",
"name" : "Comune di Fonte",
"reference" : "CnRtAAAA7KhFKu4Vg-Atk-hXMWbHWLeU62PrPHi880XdsCWddlSf2nTv7Mey3ZsrlCvXVGe52AtB2l8L5uAjFtR4TpLqafB0TV6lNP8hzkitrWh9-gsJQM7jn8im3bSiDQzbHzXZscgMTJKt0kmAbsyex5egTBIQeu-p2rCUf8jGMLLP9wB3PhoUekbrRlXXneJijLmD2wB8k59WP3U",
"types" : [ "city_hall", "local_government_office", "establishment" ],
"vicinity" : "Via Montegrappa, 17, Fonte"
},
{
"geometry" : {
"location" : {
"lat" : 45.7871870,
"lng" : 11.8734520
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
"id" : "5d2f83a7c40e72a0c400fef794e4c8dfe520a723",
"name" : "Bed and Breakfast Ca'Rinaldi",
"reference" : "CoQBewAAAFwVhcSNnYt227RZdOZyE39JyYUw8VoGIS_XvDUZTGOhs7FjqnXD2_-hb7k9KbEIadWQDJHAJm0ddm0hF5iUoCKbyLVGbRP7n7dq7V8KM5cxbApFSJsrGLc3WQMakp0W3DKcwnbaWNzgVqOYpnfEq5W08XkhUiTyLPxg1h4J1YaOEhDlAADgfmc_ATF7gWHt1nonGhRpj3UAqapKboOlwx_3QRtxu38OsA",
"types" : [ "lodging", "establishment" ],
"vicinity" : "Via Rinaldi, 13, Onè di Fonte"
},
{
"geometry" : {
"location" : {
"lat" : 45.7885340,
"lng" : 11.8701050
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "f6619b34c04252b896deb545eeec889b1d64e7c0",
"name" : "Racing Garage",
"reference" : "CnRrAAAA7Tuk7ujdu6bLwDsJwjsEhRfotYkCnYqwOFDzh_EhrRQcoRqDnoUpWdCM61PRglSFsK0EynLev2RJbS3tw6oJ-pedYVDK3vb2cnzgJNtS47YANu94H3dPsM7PT2ywC7XYm6VpfZvccOZkqdysJh43qBIQATVOV5qvwxo7wVX2tlGMfxoUpD1NShb3JsCPUf8SIsBEbwUSCdE",
"types" : [ "establishment" ],
"vicinity" : "Via Montegrappa, 1/a, Onè di Fonte"
},
{
"geometry" : {
"location" : {
"lat" : 45.786980,
"lng" : 11.873980
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "bf06566a0737faf3e434c46fda0973080a29ebc9",
"name" : "Ballestrin Autotrasporti Di Forner Adriana & C. S.N.C.",
"reference" : "CqQBlgAAAOrRTbrgM15J59QMPbFPFttJUVWfyzzwklGh7Jo3A-xvBnq4CvTZb0UkgakPlO6JbZ-srXw9YtmnPqtBBTJgDzASfkxJxLiY9tZvP8OJLNq48QS8T2DHAYrA4gGc79tUl4UL9Nub6QyQARD7-6hrchkdX4rUd_ncFMUB00oCqFVqgk6Iv04sWXYkBRPEYb_D67qlsONDVHv9fP2LMoN6F60SEK3Zb9aJre5l_aOO_mknIisaFKVI6-kPl_Ym9lceCzAQOGm-RYV9",
"types" : [ "establishment" ],
"vicinity" : "2 Via Rinaldi, Onè Di Fonte, TV 31010"
},
{
"geometry" : {
"location" : {
"lat" : 45.788760,
"lng" : 11.869970
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "90be18144ba929aef7519296aa7429dbceb421a4",
"name" : "Brolese Dr. Pio Pietro",
"reference" : "CoQBdQAAADj6BMOZy49lPRPkFdQCrxXNkoAyoK6qR2YQHOvPFsLkg_Z5Vltf6ZNHC_BUXT1BwnG0CdEj5fspWsD5d2_QsYdURtijEAB2denZEPu4rKkrQFFWEsYhD0w9xYJNYNP_IuW39ZHm9KFuUabvyCLMRhA9FSu2xOda6qjqEImVDX-2EhC27JbzmttxqoD8GE45DfOtGhTFkVIflzEoCqza8ryc3Ls6aCtJZg",
"types" : [ "establishment" ],
"vicinity" : "4 Via Monte Forcelle, Onè Di Fonte, TV 31010"
},
{
"geometry" : {
"location" : {
"lat" : 45.7879990,
"lng" : 11.8717090
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "3008c8f50b72c3d9e2f6276d177f8fe11e5158b9",
"name" : "La Tavola Illustrata",
"reference" : "CoQBcgAAABDMq2YjalP7SHbMybwclbYZauTG5DGDP-sR5piYeBAW6UsOgnAXx5H9qy4GHeYJ-pS1GCTrxBBVX8zd8oYNRfgUBgE8XLl-4GlV8ZUgsu2EQZA19KjUtVGL6hHZVX1NDVMUFYj0KGqgwu9fVqb9eDFqmC7QgzZELO4JWnz518ZcEhDJQy4zJ6p3i7RswDTSqEtOGhSimZApgUNp-Suc_W6JItum518Y6A",
"types" : [ "establishment" ],
"vicinity" : "Via Tenente Ceccato, 22, Fonte"
}
],
"status" : "OK"
}아래 형태의 json은 파싱을 잘 해올 수 있습니다. array와 object가 구분되어있네요 ㅠ
JSONObject json = jParser.getJSONFromUrl("url);
try{
JSONArray jArray = json.getJSONArray("results");
for(int i = 0; i<jArray.length(); i++){
JSONObject jj = jArray.getJSONObject(i);
image = jj.getString("icon");
JSONObject jj1 = jj.getJSONObject("geometry");
JSONObject jj2 = jj1.getJSONObject("location");
name = jj2.getString("lat");
id = jj2.getString("lng");
이런식으로 파싱을 해오면 잘 됩니다.근데 위의 형태는 array에 키값이 지정되어있지 않아서 어떻게 해야할지 잘 모르겠습니다. ㅠ
위의 형태와 같은 array 키값이 없는 json은 어떻게 파싱해와야할까요?
TestJSON response = restTemplate.getForObject(url, TestJSON.class);
public class TestJSON {
private String FOOD_ID;
....
....
get, set 오버라이드
}
스프링 안드로이드 ㄱㄱ 단 두줄이면 코딩끝..^^ 라이브러리 추가하고 오브젝트 클래스 셋팅하고 좀만 찾아보세요 쉬워요^^
아 이거 해결했습니다. ^^
JSONParser 클래스를 따로 지정했었는데요
클래스 내부에서
getJSONArryaDataFromURL
getJSONObjectDataFromURL 이렇게 나누어서
다른식으로 값을 불러오게 하니 가능하더군요
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONArray jArray = null;
static String json = "";
static JSONObject jOBj = null;
// constructor
public JSONParser() {
}
public JSONArray getJSONWithoutArrayNameFromUrl("String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jArray = new JSONArray(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jArray;
}
public JSONObject getJSONObjectFromUrl("String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jOBj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jOBj;
}
}




JSONArray객체의 getJSONObject(index) 메소드를 이용하시면 됩니다.