Retrofit Gson을 통해 JSON 개체를 구문 분석하는 방법을 배웠지 만 Retrofit Gson을 통해 전체 JSON 배열을 구문 분석해야합니다.
다음을 구문 분석해야합니다.
"{\n" +
" \"snappedPoints\": [\n" +
" {\n" +
" \"location\": {\n" +
" \"latitude\": -35.2784167,\n" +
" \"longitude\": 149.1294692\n" +
" },\n" +
" \"originalIndex\": 0,\n" +
" \"placeId\": \"ChIJoR7CemhNFmsRQB9QbW7qABM\"\n" +
" },\n" +
" {\n" +
" \"location\": {\n" +
" \"latitude\": -35.280321693840129,\n" +
" \"longitude\": 149.12908274880189\n" +
" },\n" +
" \"originalIndex\": 1,\n" +
" \"placeId\": \"ChIJiy6YT2hNFmsRkHZAbW7qABM\"\n" +
" },\n" +
" {\n" +
" \"location\": {\n" +
" \"latitude\": -35.280960897210818,\n" +
" \"longitude\": 149.1293250692261\n" +
" },\n" +
" \"originalIndex\": 2,\n" +
" \"placeId\": \"ChIJW9R7smlNFmsRMH1AbW7qABM\"\n" +
" },\n" +
" {\n" +
" \"location\": {\n" +
" \"latitude\": -35.28142839817933,\n" +
" \"longitude\": 149.1298619971291\n" +
" },\n" +
" \"originalIndex\": 3,\n" +
" \"placeId\": \"ChIJy8c0r2lNFmsRQEZUbW7qABM\"\n" +
" },\n" +
" {\n" +
" \"location\": {\n" +
" \"latitude\": -35.28193988170618,\n" +
" \"longitude\": 149.13001013387623\n" +
" },\n" +
" \"originalIndex\": 4,\n" +
" \"placeId\": \"ChIJ58xCoGlNFmsRUEZUbW7qABM\"\n" +
" },\n" +
" {\n" +
" \"location\": {\n" +
" \"latitude\": -35.282819705480151,\n" +
" \"longitude\": 149.1295597114644\n" +
" },\n" +
" \"originalIndex\": 5,\n" +
" \"placeId\": \"ChIJabjuhGlNFmsREIxAbW7qABM\"\n" +
" },\n" +
" {\n" +
" \"location\": {\n" +
" \"latitude\": -35.283139388422363,\n" +
" \"longitude\": 149.12895618087012\n" +
" },\n" +
" \"originalIndex\": 6,\n" +
" \"placeId\": \"ChIJ1Wi6I2pNFmsRQL9GbW7qABM\"\n" +
" },\n" +
" {\n" +
" \"location\": {\n" +
" \"latitude\": -35.284728724835304,\n" +
" \"longitude\": 149.12835061713685\n" +
" },\n" +
" \"originalIndex\": 7,\n" +
" \"placeId\": \"ChIJW5JAZmpNFmsRegG0-Jc80sM\"\n" +
" }\n" +
" ]\n" +
"}"
위도와 경도 만 필요합니다.
Retrofit GSON을 통해 JSON 개체를 구문 분석하는 방법은 다음과 같습니다.
package com.example.akshay.retrofitgson;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Akshay on 9/6/2015.
*/
public class gitmodel {
@SerializedName("latitude")
@Expose
public String latitude;
@SerializedName("longitude")
@Expose
public String longitude;
public void setLatitude(String latitude)
{
this.latitude = latitude;
}
public String getLatitude()
{
return latitude;
}
public void setLongitude(String longitude)
{
this.longitude = longitude;
}
public String getlatitude()
{
return latitude;
}
}
위도 / 경도 만 필요할 수도 있지만 가장 쉬운 방법은 POJO를 설정하여 모든 것을 얻은 다음 POJO에서 위도 경도를 추출하는 것입니다. 원하는 경우 내부 개체를 숨기도록 역 직렬화 된 개체를 디자인 할 수도 있습니다. JSON의 경우 매우 쉽습니다. 다음과 같이하십시오.
public static class SnappedPoints {
private List<Point> snappedPoints;
public String toString() {
return snappedPoints.toString();
}
}
public static class Point {
private Location location;
public double getLatitude() {
return location.getLatitude();
}
public double getLongitude() {
return location.getLongitude();
}
public String toString() {
return "{" + location.getLatitude() + "," + location.getLongitude() + "}";
}
}
public static class Location {
double latitude;
double longitude;
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
}
다음과 같이 간단하게 수행 할 수 있습니다.
public static void main(String[] args) {
System.out.println(new Gson().fromJson(json, SnappedPoints.class));
}
또는 Retrofit의 경우 다음과 같습니다.
public interface MyRetrofitAPI {
@GET("/path/to/json")
void getSnappedPoints(/* arguments */, Callback<SnappedPoints> callback);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다