Reputation: 21
I'm working with php on the backend and C# on client side. So when I receive multiline message from server in JSON I do this:
dynamic answer = JsonConvert.DeserializeObject(resultHttpPost);
string body = answer.body;
But I'm getting an error:
Symbol of new line is in constant
Because of this I can't use multilines messages.
I tried like this and it didn't work either
resultHttpPost = resultHttpPost.Replace("\\n", "\n").Replace("\\r", "\r").Replace("\\t", "\t");
project.Variables["var_dump"].Value = resultHttpPost;
dynamic answer = JsonConvert.DeserializeObject(resultHttpPost);
So how do I make it work?
Example for JSON:
{"status":"response_ok","message":{"body":"Hi test,\r\n\r\ntesting it, lorem ipsum lorem ipsumlorem ipsumlorem ipsumlorem ipsum.\r\n\r\nSignature","id":1015,"id_thread":741},"id_thread":741}
Upvotes: 0
Views: 7940
Reputation: 6170
I notice you're using this with ASP.NET.
It is most likely because you are actually serializing it twice. In my case, I was calling Json.NET's serialize method and then the resulting string was being returned from a Web API controller and that itself was serializing the JSON string (ASP.NET Web API internally uses Json.NET) and serializing JSON into JSON results in the \r\n
appearing everywhere.
Upvotes: 4
Reputation: 5764
Example code:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Message
{
public string body { get; set; }
public int id { get; set; }
public int id_thread { get; set; }
}
public class RootObject
{
public string status { get; set; }
public Message message { get; set; }
public int id_thread { get; set; }
}
public class Program
{
static public void Main()
{
string j = "{\"status\":\"response_ok\",\"message\":{\"body\":\"Hi test,\r\n\r\ntesting it, lorem ipsum lorem ipsumlorem ipsumlorem ipsumlorem ipsum.\r\n\r\nSignature\",\"id\":1015,\"id_thread\":741},\"id_thread\":741} ";
RootObject ro = JsonConvert.DeserializeObject<RootObject>(j);
Console.WriteLine(ro.message.body);
}
}
Result:
Hi test,
testing it, lorem ipsum lorem ipsumlorem ipsumlorem ipsumlorem ipsum.
Signature
Is exaple json problematic json?
Upvotes: 1