-2

I just have a quick question. I have this JSON string in C#, and I'm wondering if it's correct.

 var json_data1 = "{\"serverName\": \"" + ServerStats.serverName +
               "\",\"cpuLoad\":\" " + ServerStats.cpuLoad +
               "\",\"memLoad\":\" " + ServerStats.memoryLoad +
               "\",\"drive0\":\" " + ServerStats.drive0 +
               "\",\"drive1\":\" " + ServerStats.drive1 +
               "\",\"drive2\":\" " + ServerStats.drive2 +
               "\",\"drive3\":\" " + ServerStats.drive3 +
               "\",\"drive4\":\" " + ServerStats.drive4 +
               "\",\"service0\":\" " + ServerStats.testService0 +
               "\",\"service1\":\" " + ServerStats.testService1 +
               "\",\"service2\":\" " + ServerStats.testService2 +
               "\",\"lastBoot\":\" " + ServerStats.lastBoot + "\"}"; 

Is there anything wrong syntax wise with my JSON string? The reason I'm asking is that the JSON string posts to my API if I take out ",lastBoot:" + ServerStats.lastBoot, but when that line is added into the JSON string, it does not work. Any ideas? Thank you for your help.

EDIT: I updated my JSON string above, will this one work?


coderblogger
  • 84
  • 11
  • 3
    Yes, you need quotes around everything. You'd be better off using a serialiser, like [newtonsoft](http://www.newtonsoft.com/json), to do this work for you rather than manually constructing strings. For validating things, get the contents of `json_data1`, and stick it in http://jsonlint.com/ – James Thorpe Feb 05 '16 at 10:25
  • 1
    FYI, use a [StringBuilder](https://msdn.microsoft.com/en-GB/library/system.text.stringbuilder(v=vs.110).aspx) – Liam Feb 05 '16 at 10:25
  • Is it a possibility for you to use a Json parser like Json.Net: http://www.newtonsoft.com/json ? – Thomas Feb 05 '16 at 10:28
  • If you produce your Json you can validate it using [JsonLint](http://jsonlint.com/) – Liam Feb 05 '16 at 10:31
  • @Thomas I will check out newtonsoft now. Thank you. I have edited the json string, as I uploaded the wrong one. – coderblogger Feb 05 '16 at 10:31
  • 1
    _"it does not work"_ -> in what way? – James Thorpe Feb 05 '16 at 10:31
  • @JamesThorpe, is in it does not post to my api. And like said too, I will check newtonsoft :) – coderblogger Feb 05 '16 at 10:34
  • Can you post the final result, i.e. the value of `json_data1` ? – Rui Feb 05 '16 at 10:50
  • @Rui, this is what the final result: `{ "ID": 9, "serverName": "ckhgjhghkjg", "cpuLoad": "10", "memLoad": 12, "drive0": 8, "drive1": 9, "drive2": 10, "drive3": 13, "drive4": 16, "service0": "testService0", "service1": "testService1", "service2": "testService2", "lastBoot": "20-10-2013 09:00:00", "Stamp": "Feb 5 2016 12:24PM" }` – coderblogger Feb 08 '16 at 09:49
  • @avantvous Seems like valid json, I checked it here: http://www.jsoneditoronline.org/. It might be the case that your problem is elsewhere – Rui Feb 08 '16 at 10:32

1 Answers1

0

If your intent on building this your self and not using a parser then this should do it:

Stringbuilder sb = new StringBuilder();
sb.Append("{");
sb.AppendFormat("serverName:'{0}',", ServerStats.serverName);
...etc
sb.Append("}");
var json_data1 = sb.ToString();

your json should be in the format

{serverName:'myserverName', cpuLoad:'myCpuLoad',....}

You can validate you Json using JsonLint

Community
  • 1
  • 1
Liam
  • 27,717
  • 28
  • 128
  • 190