将JSON反序列化反序列化为其原始副本的$ ref引用 [英] Retrofit JSON deserializing object's $ref reference to its original copy

查看:737
本文介绍了将JSON反序列化反序列化为其原始副本的$ ref引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft.Net和Breeze for API,我使用Retrofit获得的结果嵌套了重复的相同对象。例如,EmployeeJob具有Customer导航属性,因此API结果如下所示

I am using Microsoft.Net with Breeze for APIs and the results I get using Retrofit have nested repeated same objects. For example EmployeeJob has Customer navigation property so the APIs result looks like this

 {
   Id:1,
   "Customer_Id": 39,
    "Customer": {
        "$id": "2",
        "$type": "Wit.Trade.Entities.Customer, Wit.Trade",
        "CourtesyTitle": "Mr",
        "FirstName": "Ahmad"
    }
}
{
  Id:2
  "Customer_Id": 39,
    "Customer": {
        "$ref": "2" //here same customer Ahmad
    },
}

现在Java 列表我得到了这些 EmployeeJobs 在第一条记录中只有 Customer ,其他则什么也没有。如何将 $ ref:2映射到其原始值而不是 $ ref

Now the Java List I get of these EmployeeJobs has only Customer in the first record and others have nothing. How can I map the $ref:"2" to its original value instead of this $ref.

我不希望我的服务器API出于网络和性能原因发送完整的对象,这就是为什么我要反序列化这些 $ refs 在客户端就像 Angularjs $ resource service 为我们做的。

I don't want my server APIs to send the complete objects for network and performance reasons, that's why I want to deserialize these $refs on client side just like Angularjs $resource service does for us.

推荐答案

目前我已经为这个$ ref解决方案手动工作了

Currently I have worked arround manually for the $ref solution like this

//========== $ref manual solution for employee jobs' customers
            List<Customer> completedCustomers = new ArrayList<>();
            for (EmployeeJob empJob : empJobs) {
                if (empJob.Customer != null && empJob.Customer.Id == null && empJob .Customer.$ref != null) {
                    for (Customer comCus : completedCustomers) {
                        if (comCus.$id.equalsIgnoreCase(empJob.Customer.$ref))
                            empJob.Customer = comCus;
                    }
                } else
                    completedCustomers.add(empJob.Customer);
            }

现在 empJobs 有$ refs替换为相应的客户。

Now empJobs has the $refs replaced with their corresponding Customers.

这篇关于将JSON反序列化反序列化为其原始副本的$ ref引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆