开发者

write to specific position in .json file + serilaize size limit issue C#

开发者 https://www.devze.com 2023-04-07 00:37 出处:网络
I have a method that retrieves data from a json serialized string and writes it to a .json file using:

I have a method that retrieves data from a json serialized string and writes it to a .json file using:

TextWriter writer = new StreamWriter("~/example.json");
writer2.Write("{\"Names\":" + new J开发者_JAVA百科avaScriptSerializer().Serialize(jsonData) + "}");   

data(sample):

{"People":{"Quantity":"4"}, ,"info" :
 [{"Name":"John","Age":"22"}, {"Name":"Jack","Age":"56"}, {"Name":"John","Age":"82"},{"Name":"Jack","Age":"95"}]
}

This works perfectly however the jsonData variable has content that is updated frequently. Instead of always deleting and creating a new example.json when the method is invoked,

  • Is there a way to write data only to a specific location in the file? in the above example say to the info section by appending another {"Name":"x","Age":"y"}?

My reasoning for this is I ran into an issue when trying to serialize a large amount of data using visual studio in C#. I got "The length of the string exceeds the value set on the maxJsonLength property” error. I tried to increase the max allowed size in the web.config using a few suggested methods in this forum but they never worked. As the file gets larger I feel I may run into the same issue again. Any other alternatives are always welcome. Thanks in advance.


I am not aware of a JSON serializer that works with chunks of JSON only. You may try using Json.NET which should work with larger data:

var data = JsonConvert.SerializeObject(new { Names = jsonData });
File.WriteAllText("example.json", data);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号