c# JsonConvert SerializeObject fails

Permudious

New Member
Dec 28, 2014
19
0
Good evening,

I am developing a Web API for the first time, where I should like expect a JSON output.
Whatever I try with a return I still have an invalid response.

This is the output:
"[{\"TemperatureAquarium\":\"27.0\",\"TemperatureAquariumShrt\":\"27\",\"TemperatureSump\":\"26.7\",\"TemperatureSumpShrt\":\"26\"}]"

But in my opinion it should be;
[{"TemperatureAquarium":"27.0","TemperatureAquariumShrt":"27","TemperatureSump":"26.7","TemperatureSumpShrt":"26" }]

I already tried things like replace(@"\","") or replace("\\", string.empty) for removing the backslashes, but they still got there.
Even when I add the option Formatting.Intended it goes wrong.

the return code is; return JsonConvert.SerializeObject(TemperatureList);

What do I wrong?
 

Permudious

New Member
Dec 28, 2014
19
0
Hereby the code

C#:
 csResponse response = new csResponse();

public class csResponse
{
    public int StatusCode { get; set; }
    public string ErrorMessage { get; set; }
}

public class csTemperature
{
    public string TemperatureAquarium { get; set; }
    public string TemperatureAquariumShrt { get; set; }
    public string TemperatureSump { get; set; }
    public string TemperatureSumpShrt { get; set; }
}

 public string GET_Temperature()
 {
     try
     {
         string contents = "";

         using (WebClient client = new WebClient())
         {
             contents = client.DownloadString("http://192.168.2.10/");
         }

         List<csTemperature> TemperatureList = new List<csTemperature>();

         csTemperature temp = new csTemperature();
         temp.TemperatureAquariumShrt = contents.Substring(117, 2);
         temp.TemperatureAquarium = contents.Substring(117, 4);
         temp.TemperatureSumpShrt = contents.Substring(148, 2);
         temp.TemperatureSump = contents.Substring(148, 4);
         TemperatureList.Add(temp);

         response.StatusCode = 200;
         return JsonConvert.SerializeObject(TemperatureList, Formatting.Indented);
 
     }
     catch
     {
         response.StatusCode = 100;
         response.ErrorMessage = "No data found";
         return JsonConvert.SerializeObject(response);
     }
 }
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456

Try this, looks like this is the issue that's going on.
 

Permudious

New Member
Dec 28, 2014
19
0

Try this, looks like this is the issue that's going on.
Seems like I have to use JToken.Parse.

But when I do that, I still got some backslashes and \r\n as well

C#:
string json = JsonConvert.SerializeObject(TemperatureList, Formatting.Indented);
var jsonreturn = JToken.Parse(json);
return jsonreturn.ToString();

JSON:
"[\r\n  {\r\n    \"TemperatureAquarium\": \"27.0\",\r\n    \"TemperatureAquariumShrt\": \"27\",\r\n    \"TemperatureSump\": \"26.8\",\r\n    \"TemperatureSumpShrt\": \"26\"\r\n  }\r\n]"
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
Seems like I have to use JToken.Parse.

But when I do that, I still got some backslashes and \r\n as well

C#:
string json = JsonConvert.SerializeObject(TemperatureList, Formatting.Indented);
var jsonreturn = JToken.Parse(json);
return jsonreturn.ToString();

JSON:
"[\r\n  {\r\n    \"TemperatureAquarium\": \"27.0\",\r\n    \"TemperatureAquariumShrt\": \"27\",\r\n    \"TemperatureSump\": \"26.8\",\r\n    \"TemperatureSumpShrt\": \"26\"\r\n  }\r\n]"
Did you try the previous solution? I'm not sure what you are trying to do. Parsing is only to take it back from JSON to something else. I high;y recommend looking into the terms. I have a feeling you aren't really aware of what you are doing. It's important to actually understand the functions you are using.
 

Permudious

New Member
Dec 28, 2014
19
0
Did you try the previous solution? I'm not sure what you are trying to do. Parsing is only to take it back from JSON to something else. I high;y recommend looking into the terms. I have a feeling you aren't really aware of what you are doing. It's important to actually understand the functions you are using.
Not sure I could use that one.

What I am trying is bind 4 kind of values (27.0, 27, 26.8, 26) to names ( TemperatureAquarium, TemperatureAquariumShrt, TemperatureSump, TemperatureSumpShrt) I can add them static for now, but somehow they have to add in a list first right? before I can serialize this to get a JSON output.
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
As there's no double serialization in the code you shared, it's happening somewhere else. Those backslashes are 100% caused by double serialization. Please share the code that's calling this GET_Temp function, and anything else that it's going through.
 

Users who are viewing this thread

Top