lcs*_*bdr 5 java android json deserialization flexjson
我目前正在尝试使用Flexjson反序列化JSON字符串并将其映射到我的Android应用程序的Object模型.该应用程序是一种具有多个供应商的库,其中可以包含一些目录,其中包含更多目录和文档.json是从我没有影响的Web服务中获取的,看起来像这样:
{
    "library":{
        "vendor":[
            {
                "id":146,
                "title":"Vendor1",
                "catalog":[
                     {
                        "id":847,
                        "document":[
                            {
                                "id":1628,
                                "title":"Document",
                ...
                            },
                            {
                ...
                            }
                        ],
                        "title":"Catalog ",
                    },
                {
                ...
                }
              ]
            },
            {
            ...
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)
因此,每个供应商,目录,文档都由JSONObject表示,所有子目录和文档都在JSONArray中.到目前为止,一切都适用于Flexjson和以下反序列化代码:
LibraryResponse response = new JSONDeserializer<LibraryResponse>()
                    .use(Timestamp.class, new TimestampObjectFactory())
                    .deserialize(getLocalLibrary(), LibraryResponse.class);
return response.library;
Run Code Online (Sandbox Code Playgroud)
我有一个库对象有一个List<Vendor>.每个供应商都有一个List<Catalog>和一个List<Document>.
但遗憾的是,如果目录只包含单个文档或目录只包含一个目录,那么Web服务会将JSONArrays绑定到简单的JSONObjects.所以在这种情况下json看起来像这样:
"document":
    {
        "id":1628, 
        "title":"Document",
        ...
    }
Run Code Online (Sandbox Code Playgroud)
现在Flexjson不知道如何反序列化,最终我得到了一个库List<HashMap>而不是一个库.vendorX.getDocument()List<Document>.一个想法是明确告诉Flexjson如何处理这种情况,但我不知道从哪里开始.另一种方法是手动解析初始json并用适当的JSONArray替换这些JSONObject.但我觉得这样做并不是很好,因为图书馆可以很深.
我希望你能在这里提供一些指导.
哎呀,这是一些粗糙的 json 映射。哪个后端编码员做到了?!#没有帮助。
从代码来看,Flexjson 的编码可以开箱即用地处理这个问题。但看起来它没有将类型信息传递给绑定,因此它不知道它绑定到什么类型,因此它只返回一个 Map。这是一个应该修复的 错误。好消息是有一个解决方法。
不管怎样,我能想到的最简单的事情就是在该列表上安装一个ObjectFactory。然后您可以检查反序列化流时是否获得了 Map 或 List。然后您可以将其包装在列表中并将其发送到适当的解码器。就像是:
LibraryResponse response = new JSONDeserializer<LibraryResponse>()
                .use(Timestamp.class, new TimestampObjectFactory())
                .use("library.vendor.values.catalog.values.document", new ListDocumentFactory() )
                .deserialize(getLocalLibrary(), LibraryResponse.class);
Run Code Online (Sandbox Code Playgroud)
然后
public class ListDocumentFactory implements ObjectFactory {
     public Object instantiate(ObjectBinder context, Object value, Type targetType, Class targetClass) {
         if( value instanceof Collection ) {
             return context.bindIntoCollection((Collection)value, new ArrayList(), targetType);
         } else {
             List collection = new ArrayList();
             if( targetType instanceof ParameterizedType ) {
                 ParameterizedType ptype = (ParameterizedType) targetType;
                 collection.add( context.bind(value, ptype.getActualTypeArguments()[0]) );
             } else {
                 collection.add( context.bind( value ) );
                 return collection;
             }
         }
     }
}
Run Code Online (Sandbox Code Playgroud)
我认为这大致可以解决该错误,但也应该解决您的问题。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           1046 次  |  
        
|   最近记录:  |