简体   繁体   English

将JSON反序列化反序列化为其原始副本的$ ref引用

[英]Retrofit JSON deserializing object's $ref reference to its original copy

I am using Microsoft.Net with Breeze for APIs and the results I get using Retrofit have nested repeated same objects. 我使用Microsoft.Net和Breeze for API,我使用Retrofit得到的结果嵌套了重复的相同对象。 For example EmployeeJob has Customer navigation property so the APIs result looks like this 例如,EmployeeJob具有Customer导航属性,因此API结果如下所示

 {
   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
    },
}

Now the Java List I get of these EmployeeJobs has only Customer in the first record and others have nothing. 现在我得到的这些EmployeeJobs的Java List在第一条记录中只有Customer ,而其他的则没有。 How can I map the $ref:"2" to its original value instead of this $ref . 如何将$ref:"2"映射到其原始值而不是$ref

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. 我不希望我的服务器API出于网络和性能原因发送完整的对象,这就是为什么我想在客户端反序列化这些$refs ,就像Angularjs $resource service为我们做的那样。

Currently I have worked arround manually for the $ref solution like this 目前我已经手动为这个$ ref解决方案工作了arround

//========== $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);
            }

Now empJobs has the $refs replaced with their corresponding Customers. 现在empJobs已将$ refs替换为相应的客户。

将 json 反序列化为列表<object><div id="text_translate"><p>我有 json:</p><pre> "taxLevels": [{ "code": "VAT", "percentage": 19.0 } ]</pre><p> 这确实是List&lt;TTaxLevel&gt;</p><p> 我有<strong>Model.class</strong> :</p><pre> public class Model{ private final List&lt;TTaxLevel&gt; taxLevels; }</pre><p> 和<strong>TtaxLevel.class</strong> :</p><pre> @NoArgsConstructor public class TTaxLevel { private String code; private Double percentage; }</pre><p> 但我在这里收到错误:</p><blockquote><p> [Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.LinkedHashMap&lt;java.lang.String,java.lang.String&gt; out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.LinkedHashMap&lt;java.lang.String,java.lang.String&gt; out of START_ARRAY token at [Source: (PushbackInputStream); 行:36,列:19](通过参考链:...Model["taxLevels"])]]</p></blockquote><p> 我可以以某种方式强制jackson在这里ArrayList而不是Map吗? 这是一个问题。</p><p> 这是反序列化代码:</p><pre> Model model = new ObjectMapper().readValue(content, Model.class);</pre></div></object> - Deserializing json as List<Object>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 java 中重新分配其复制引用时更改原始对象 - Change original object when reassigning its copy-reference in java 使用GSON和RETROFIT反序列化复杂的JSON对象 - Deserializing complex JSON object with GSON and RETROFIT 使用Retrofit GsonConverterFactory反序列化为false或对象的json - Deserializing json that is false or object with Retrofit GsonConverterFactory 通过序列化为JSON并立即反序列化来深度复制对象 - Deep copy an object by serializing to JSON and immediately deserializing 使用Gson反序列化JSON时引用父对象 - Reference parent object while deserializing a JSON using Gson 如何使用对象的原始 class 投射引用? - How to cast a reference with an object's original class? 反序列化以对象ID作为对象名称的JSON对象列表 - Deserializing a list of JSON objects having object's ID as an object name 反序列化JSON对象 - Deserializing JSON object 将 json 反序列化为列表<object><div id="text_translate"><p>我有 json:</p><pre> "taxLevels": [{ "code": "VAT", "percentage": 19.0 } ]</pre><p> 这确实是List&lt;TTaxLevel&gt;</p><p> 我有<strong>Model.class</strong> :</p><pre> public class Model{ private final List&lt;TTaxLevel&gt; taxLevels; }</pre><p> 和<strong>TtaxLevel.class</strong> :</p><pre> @NoArgsConstructor public class TTaxLevel { private String code; private Double percentage; }</pre><p> 但我在这里收到错误:</p><blockquote><p> [Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.LinkedHashMap&lt;java.lang.String,java.lang.String&gt; out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.LinkedHashMap&lt;java.lang.String,java.lang.String&gt; out of START_ARRAY token at [Source: (PushbackInputStream); 行:36,列:19](通过参考链:...Model["taxLevels"])]]</p></blockquote><p> 我可以以某种方式强制jackson在这里ArrayList而不是Map吗? 这是一个问题。</p><p> 这是反序列化代码:</p><pre> Model model = new ObjectMapper().readValue(content, Model.class);</pre></div></object> - Deserializing json as List<Object> 嵌套JSON对象(改进) - Nested JSON Object (Retrofit)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM