0

我正在尝试从服务器反序列化 JSON,它有时会省略某些键/值,这使我无法使用它:

var obj = JsonConvert.Deserialize<Obj>(respString);

我阅读了 JSON.net 文档并继续他的 Linq to JSON 反序列化,如下所示:

string respString = await resp.Content.ReadAsStringAsync();
JObject json = JObject.Parse(respString);

...

CustomObject.child.primary_key = int.Parse(obj[i]["primary_key"].ToString());
CustomObject.foreign_key = obj[i]["foreign_key"].ToString();
CustomObject.properties = int.Parse(ojb[i]["properties"].ToString());

try //this is being done a few times throughout the loop
{
    CustomObject.unreliableProperty = obj[i]["xxx"].ToString();
}
Catch { }

一切正常,但我偶尔会得到outofmemoryexceptions(来自服务器的 JSON 很大)。我已经尝试JsonConverter从 JSON.net 实现自定义,但是使用嵌套层次结构(3 级深度),JsonConverter变得太复杂且难以理解,更不用说维护了。

当我运行内存分析器时,我看到超过 150mb 的峰值,这将导致应用程序出现 OOM 异常。在堆部分下,我看到 30-40% 的内存分配是由mscorlib字符串分配完成的。另一个问题是在循环过程中,有连续的 GC 连续进行。我对它现在的写作方式感到非常厌恶,但我已经无能为力了,有人能帮忙吗?

4

0 回答 0