在C#中读取大型Json文件

时间:2015-10-17 13:07:03

标签: json c#-4.0 json.net

我正在尝试在C#中读取一个大的Json(120Mb)文件。我正在获得Memory Over flow异常。我使用以下代码。

// Load the JObject directly from a file
using (var streamReader = File.OpenText(fileName))
using (var jsonReader = new JsonTextReader(streamReader))
{
    obj = JObject.Load(jsonReader);
}

// Rename all properties with primitive values (string, number, boolean, null) to begin with "@"
foreach (var o in obj.Descendants().OfType<JObject>())
{
  // Attributes must appear first in the JObject's property list.
int insertIndex = 0;
foreach (var property in o.Properties().Where((p => p.Value is JValue && !p.Name.StartsWith("@"))).ToList())
{
    property.Remove();
    ((IList<JToken>)o).Insert(insertIndex++, new JProperty("@" + property.Name, property.Value));
}

}

// Convert to XmlDocument
XmlDocument doc;
using (var reader = obj.CreateReader())
{
    doc = (XmlDocument)JsonExtensions.DeserializeXmlNode(reader);
}

0 个答案:

没有答案