Java 如何使用改型解析json

Java 如何使用改型解析json,java,android,json,retrofit,realm,Java,Android,Json,Retrofit,Realm,我对安卓系统相当陌生,使用了改型和领域。我很难从url中正确解析这个json字符串。 以下是我迄今为止所做的工作 我的界面: package com.example.stephen.traveland.Rest; import android.database.Observable; import com.example.stephen.traveland.Models.Country; import java.util.List; import retrofit.http.GET; publ

我对安卓系统相当陌生,使用了改型和领域。我很难从url中正确解析这个json字符串。 以下是我迄今为止所做的工作

我的界面:

package com.example.stephen.traveland.Rest;
import android.database.Observable;
import com.example.stephen.traveland.Models.Country;
import java.util.List;
import retrofit.http.GET;

public interface TravelAnDInterface {
    @GET("/index-updated.json/")
    Observable<List<Country>> getAllCountries();
}
package com.example.stephen.traveland.Rest;
导入android.database.Observable;
导入com.example.stephen.traveland.Models.Country;
导入java.util.List;
导入reformation.http.GET;
公共接口TravelAnDInterface{
@获取(“/index updated.json/”)
可观察的getAllCountries();
}
这个java类就是我使用接口从url中提取字符串的地方

package com.example.stephen.traveland.Rest;
import android.content.Context;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.squareup.okhttp.OkHttpClient;

import io.realm.RealmObject;
import retrofit.RestAdapter;
import retrofit.android.AndroidLog;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;

public class TravelAnD {
    private static final String URL = "http://data.international.gc.ca/travel-voyage/";
    private final static Gson gson = new GsonBuilder()
        .setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return f.getDeclaringClass().equals(RealmObject.class);
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        })
        .create();

    private static TravelAnDInterface sTravelAnDSvc;

    public static TravelAnDInterface getCountries() {

        if (sTravelAnDSvc == null) {
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint(URL)
                    .setClient(new OkClient(new OkHttpClient()))
                    .setConverter(new GsonConverter(gson))
                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .setLog(new AndroidLog("RETROFIT"))
                    .build();

            sTravelAnDSvc = restAdapter.create(TravelAnDInterface.class);
        }

        return sTravelAnDSvc;
    }
}
package com.example.stephen.traveland.Rest;
导入android.content.Context;
导入com.google.gson.com策略;
导入com.google.gson.FieldAttributes;
导入com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入com.squareup.okhttp.OkHttpClient;
导入io.realm.RealmObject;
进口改装.RestAdapter;
导入reformation.android.AndroidLog;
导入reformation.client.OkClient;
进口改装.converter.GsonConverter;
公务舱旅客{
私有静态最终字符串URL=”http://data.international.gc.ca/travel-voyage/";
私有最终静态Gson Gson=new GsonBuilder()
.SetExclutionStrategy(新的ExclutionStrategy)(){
@凌驾
公共布尔值shouldSkipField(字段属性f){
返回f.getDeclaringClass().equals(RealmObject.class);
}
@凌驾
公共布尔shouldSkipClass(类clazz){
返回false;
}
})
.create();
专用静态行程和接口Straveland SVC;
公共静态TravelAnDInterface getCountries(){
if(sTravelAnDSvc==null){
RestAdapter RestAdapter=new RestAdapter.Builder()
.setEndpoint(URL)
.setClient(新的OkClient(新的OkHttpClient()))
.setConverter(新GsonConverter(gson))
.setLogLevel(RestAdapter.LogLevel.FULL)
.setLog(新的AndroidLog(“改装”))
.build();
sTravelAnDSvc=restAdapter.create(TravelAnDInterface.class);
}
返回带和SVC;
}
}
这是我的一个对象类,所以您可以看到我是如何尝试完成的

package com.example.stephen.traveland.Models;

import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;

public class Country extends RealmObject {

    @PrimaryKey
    private int country_id;
    private String country_iso;
    private String country_in_english;
    private String country_in_french;
    private int advisory_state;
    private RealmList<DatePublished> datePublished;
    private int advisory_warning;
    private int regional_advisory;
    private int content;
    private String recent_update_type;
    private RealmList<English> english;
    private RealmList<French> french;

    public Country() { super(); }

    public Country(int country_id, String country_iso, String     country_in_english, String country_in_french,
               int advisory_state, int advisory_warning, int regional_advisory, int content) {
        this.country_id = country_id;
        this.country_iso = country_iso;
        this.country_in_english = country_in_english;
        this.country_in_french = country_in_french;
        this.advisory_state = advisory_state;
        this.advisory_warning = advisory_warning;
        this.regional_advisory = regional_advisory;
        this.content = content;

    }

    public int getAdvisory_state() { return advisory_state; }

    public void setAdvisory_state(int advisory_state) { this.advisory_state = advisory_state; }

    public int getAdvisory_warning() { return advisory_warning; }

    public void setAdvisory_warning(int advisory_warning) { this.advisory_warning = advisory_warning; }

    public int getContent() { return content; }

    public void setContent(int content) { this.content = content; }

    public int getCountry_id() { return country_id; }

    public void setCountry_id(int country_id) { this.country_id = country_id; }

    public String getCountry_in_english() { return country_in_english; }

    public void setCountry_in_english(String country_in_english) { this.country_in_english = country_in_english; }

    public String getCountry_in_french() { return country_in_french; }

    public void setCountry_in_french(String country_in_french) { this.country_in_french = country_in_french; }

    public String getCountry_iso() { return country_iso; }

    public void setCountry_iso(String country_iso) {this.country_iso = country_iso; }

    public RealmList<DatePublished> getDatePublished() { return datePublished; }

    public void setDatePublished(RealmList<DatePublished> datePublished) { this.datePublished = datePublished; }

    public RealmList<English> getEnglish() { return english; }

    public void setEnglish(RealmList<English> english) {this.english = english;}

    public RealmList<French> getFrench() { return french; }

    public void setFrench(RealmList<French> french) { this.french = french; }

    public String getRecent_update_type() { return recent_update_type; }

    public void setRecent_update_type(String recent_update_type) { this.recent_update_type = recent_update_type; }

    public int getRegional_advisory() { return regional_advisory; }

    public void setRegional_advisory(int regional_advisory) { this.regional_advisory = regional_advisory; }
package com.example.stephen.traveland.Models;
导入io.realm.RealmList;
导入io.realm.RealmObject;
导入io.realm.annotations.PrimaryKey;
公共类国家扩展现实对象{
@主键
私人国际身份证;
私人字符串国家标准化组织;
私人字符串国家/地区(英语);
法语的私人字符串国家;
国家私人咨询机构;
私有RealmList已发布;
私人int咨询警告;
私人国际区域咨询;
私有int内容;
私有字符串最近更新类型;
私人地产英语;
私人地产公司;
公共国家(){super();}
公共国家/地区(国际国家/地区id、字符串国家/地区iso、英语字符串国家/地区、法语字符串国家/地区、,
国际咨询(国家、国际咨询(警告)、国际区域咨询(建议)、国际内容){
this.country\u id=country\u id;
this.country\u iso=country\u iso;
this.country\u in\u english=country\u in\u english;
this.country_in_法语=country_in_法语;
this.advisional_state=advisional_state;
this.advisional\u warning=advisional\u warning;
this.regional_advision=regional_advision;
this.content=内容;
}
public int getAdvisory_state(){return advisory_state;}
公共咨询州(int咨询州){this.advisional\u state=advisional\u state;}
public int getAdvisory_warning(){return advisory_warning;}
public void setAdvisory_warning(int advisory_warning){this.advisory_warning=advisory_warning;}
public int getContent(){return content;}
public void setContent(int-content){this.content=content;}
public int getCountry_id(){return country_id;}
public void setCountry_id(int country_id){this.country_id=country_id;}
公共字符串getCountry_in_english(){return country_in_english;}
public void setCountry_in_english(字符串country_in_english){this.country_in_english=country_in_english;}
公共字符串getCountry_in_french(){return country_in_french;}
public void setCountry_in_法语(字符串country_in_法语){this.country_in_法语=country_in_法语;}
公共字符串getCountry_iso(){返回country_iso;}
public void setCountry_iso(字符串country_iso){this.country_iso=country_iso;}
public RealmList getDatePublished(){return datePublished;}
public void setDatePublished(RealmList datePublished){this.datePublished=datePublished;}
public RealmList getEnglish(){return english;}
公共英语(RealmList英语){this.english=english;}
public RealmList getFrench(){return french;}
public void setFrench(RealmList french){this.french=french;}
公共字符串getRecent_update_type(){return recent_update_type;}
public void setRecent_update_type(字符串recent_update_type){this.recent_update_type=recent_update_type;}
public int getRegional_advisory(){return regional_advisory;}
公共区域咨询(int区域咨询){this.regional咨询=区域咨询;}
}

我已经创建了用于存储数据的类,但是有一个问题,因为字符串本身很奇怪,很难处理。我希望得到一些关于我做错了什么的指导。任何帮助都将不胜感激

@编辑 我的任何对象都不再使用Realm,之前的代码也被修改了很多,因为数据太大,无法存储在手机上。对于一台设备来说,信息太多了

但是,一个新问题或相同的问题只是在不同的情况下。我的retofit电话是grabbin
public void gatherData() {

    final ProgressDialog progress = new ProgressDialog(this);
    progress.setTitle("Please wait...");
    progress.setMessage("Loading data...");
    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progress.setIndeterminate(false);
    progress.show();

    bossModel1 = new BossModel();

    Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("http://data.international.gc.ca/travel-voyage")
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

    Api service = restAdapter.create(Api.class);

    service.listRepos(new Callback<BossModel>() {
        @Override
        public void success(BossModel bossModel, Response response) {
            bossModel1 = bossModel;

            Log.i("SUCCESS", "" + bossModel1.getData());

            for (Field field : bossModel1.getData().getClass().getDeclaredFields()) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    Log.i("ahlsdkjha: ", field.getName());
                    countryISO.add(field.getName());
                }
            }

            progress.dismiss();
        }

        @Override
        public void failure(RetrofitError error) {

        }
    });
}

public void gatherVPData(final String countryIso) {
    vpModel1 = new VPModel();

    Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("http://data.international.gc.ca/travel-voyage")
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

    Api service = restAdapter.create(Api.class);

    service.getCountry(countryIso, new Callback<VPModel>() {

        @Override
        public void success(VPModel vpModel, Response response) {
            vpModel1 = vpModel;
            Log.d("SUCCESS2: ", "" + vpModel1.getData());
        }

        @Override
        public void failure(RetrofitError error) {

        }
    });
}
public interface Api {
    @GET("/index-updated.json")
    void listRepos(Callback<BossModel> callback);

    @GET("/countries/cta-cap-{country}.json")
    void getCountry(@Path("country") String countryIso, Callback<VPModel> callback);
}
public void populateCountryList(int id) {

    for (int i = 0; i < countryISO.size() - 1; i++) {
        gatherVPData(countryISO.get(i));
        if (vpModel1.getData().getAdvisoryState() == id) {
            countries.add(new Country(
                    vpModel1.getData().getCountryIso(),
                    vpModel1.getData().getAdvisoryState(),
                    vpModel1.getData().getHasAdvisoryWarning(),
                    vpModel1.getData().getHasRegionalAdvisory(),
                    vpModel1.getData().getHasContent(),
                    vpModel1.getData().getUpdateMetadata(),
                    vpModel1.getData().getEng(),
                    vpModel1.getData().getFra()
            ));
        }
    }
}
JsonObject getAllCountries();
JsonObject json = <your adapter here>.getAllCountries();
Map<String, Country> countries = gson.fromJson(jsonResponse.getAsJsonObject("data"),
                    new TypeToken<Map<String, Country>>() {
                    }.getType());