0

json-string tmp => {"result_count":1、 "next_offset":1、 "entry_list":[{"id": "xyz123"、 "module_name": "Products"、 "name_value_list"を変換する必要があります: {"id":{"name": "id"、 "value": "xyz123"}、 "name":{"name": "name"、 "value": "test_product_2"}}}]、 "relationship_list ":[]} を対応するjava-pojoに

私のpojoは次のようになります

public class GetEntryListResponse {

public int result_count = 0;
public int next_offset = 0;
public List<EntryList> entryList = new ArrayList<EntryList>();
public static class EntryList {
    String id = "";
    String module_name = "";
    public static class NameValueList {
        public static class Id {
            String name = "";
            String value = "";
        }
        public static class Name {
            String name = "";
            String value = "";
        }
    }
}
}

脱穀作業には用途があります

Gson json_response = new Gson();
GetEntryListResponse resp = json_response.fromJson(tmp, 
                                                   GetEntryListResponse.class);

他のバリエーションも試しましたが、これが今のところ最高のようです。問題は、result_countとnext_offsetがintに変換されますが、配列entryListの型がnull値であるということです。

4

2 に答える 2

0

変更してみてください:

public List<EntryList> entryList = new ArrayList<EntryList>();

に:

public List<EntryList> entry_list= new ArrayList<EntryList>();

逆シリアル化します。

于 2012-08-13T13:48:32.763 に答える
0

クラスに InstanceCreator と JsonDeserializer を実装する

  public class GetEntryListResponse implements
            InstanceCreator<GetEntryListResponse>,
            JsonDeserializer<GetEntryListResponse>{

    @Override
        public GetEntryListResponse createInstance(Type type) {
            return this;
        }

    @Override
        public GetEntryListResponse deserialize(JsonElement json, Type typeOfT){
      json.getJsonObject();// 
    // create your classes objects here by json key
    }

と使用

GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.registerTypeAdapter(GetEntryListResponse.class,
                new GetEntryListResponse()).create();
于 2012-08-13T13:31:24.770 に答える