添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Using c# and .net 4.8 I need to combine 2 json strings. For this I'm using the JObject, but if you have a better (more efficient/faster) way to do this, please let know. this will run on the server so efficient is important.

This is what I'm doing now:

string json1 = "{\"Header1\":[[[\"id\",\"b2a30048-fdd3-490d-a8c2-0da023349aa1\"],[\"name\",\"A Cool Name\"],[\"rating\",3.3],[\"timeStamp\",\"2021-02-04T15:45:39.0844116-06:00\"]]]}";
string json2 = "{\"Header2\":[[[\"id\",\"b2a30048-fdd3-490d-a8c2-0da023349aa1\"],[\"name\",\"A Cool Name\"],[\"rating\",3.3],[\"timeStamp\",\"2021-02-04T15:45:39.0844116-06:00\"]]]}";
JObject test1 = JObject.Parse(json1);
JObject test2 = JObject.Parse(json2);
var result = new JObject();
result.Merge(test1);
result.Merge(test2);
Console.WriteLine(result);

As you can see, the json strings are compact text. However, result.ToString() looks like this:

"Header1": [ "id", "b2a30048-fdd3-490d-a8c2-0da023349aa1" "name", "A Cool Name" "rating", "timeStamp", "2021-02-04T15:45:39.0844116-06:00" "Header2": [ "id", "b2a30048-fdd3-490d-a8c2-0da023349aa1" "name", "A Cool Name" "rating", "timeStamp", "2021-02-04T15:45:39.0844116-06:00"

and if this was a large json it would be very bloated for sending across the wire.

Please let me know the most efficient way to do this getting compact results.

Thank you.

In JSON, these characters don't seem to affect the efficiency of execution, the difference is only visually different.

Are whitespace characters insignificant in JSON?

But anyway, if you don't like these characters, you can use the String.Replace Method to replace them with empty strings.

Console.WriteLine(re.Replace("\r\n","");  

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.