0

I am creating a string variable to use in an rest post call and it is failing. when I debug and look at the json value I am told it is not in json format. It sure seems to be key:value pairs so I an not sure what the issue here is?

instead of double single quotes I also tried escaping the " by using \ like so (neither method is good it seems):

//string postData = "{\"title\":\"Change Title\",  \"description\":\"Create description\",  \"scheduledStartDate\": \"2018-12-24T11:24:48.91Z\",  \"scheduledEndDate'': ''2018-12-25T11:24:48.91Z''  }";

    string postData = @"{''changeNumberForClone'': ''C03688051'',
                        ''scheduledStartDate'': ''2017-12-24T11:24:48.91Z'',
                        ''scheduledEndDate'': ''2017-12-25T11:24:48.91Z''}";        

enter image description here

4
  • 2
    You would be better off using a Dictionary or custom type, then serializing to json. Commented Dec 19, 2017 at 14:19
  • 1
    Creating the JSON by hand like this is going be very error-prone, and it won't scale at all. Create and serialize a dictionary like Crowcoder suggested. Commented Dec 19, 2017 at 14:21
  • 1
    (Your attempt to use double quotes is broken because you incorrectly switch to double single-quotes towards the end.) Commented Dec 19, 2017 at 14:22
  • var postData = "{\"changeNumberForClone\":\"C03688051\", \"scheduledStartDate\":\"2017-12-24T11:24:48.91Z\", \"scheduledEndDate\": \"2017-12-25T11:24:48.91Z\"}"; Commented Dec 19, 2017 at 14:23

1 Answer 1

4

Using NewtonSoft Json.NET, you could use the following code to obtain a correct json string:

Dictionary<String, String> jsonDict = new Dictionary<String, String>();
jsonDict.Add("changeNumberForClone", "C03688051");
jsonDict.Add("scheduledStartDate", "2017-12-24T11:24:48.91Z");
jsonDict.Add("scheduledEndDate", "2017-12-25T11:24:48.91Z");

String postData = JsonConvert.SerializeObject(jsonDict);

If you don't want to add a new library to your project:

String postData = "{\"changeNumberForClone\":\"C03688051\", \"scheduledStartDate\":\"2017-12-24T11:24:48.91Z\", \"scheduledEndDate\": \"2017-12-25T11:24:48.91Z\"}";

In order to produce json strings with multiple levels of depth using the same approach, you can use anonymous objects as follows:

var obj = new { abc = new { def = new { one="1", two="2" } } };
var json = JsonConvert.SerializeObject(obj);

or, if you prefer to use Dictionary instances:

var obj = new Dictionary<String,Object>()
{
    {
        "abc", new Dictionary<String,Object>()
        {
            {
                "def" , new Dictionary<String,Object>()
                {
                    { "one", "1" }, {"two", "2" }
                }
            }
        }
    }
};

the output, for both approaches, would be the following:

{
   "abc": {
      "def" : {
         "one": "1",
         "two": "2",
      },
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

using this method how can you add a second level of items, lets say a server list with multiple server data points under it? in other words, is dictionary only one level deep?
Dictionary<String,String> wouldn't be enough anymore. Take a look at this question: stackoverflow.com/questions/29808718/…... and this one too that make use of anonymous objects: stackoverflow.com/questions/32529169/…
I required to add [ ] in Json is it possible like format = new [ "Archive BitTorrent", "JPEG", "JPEG Thumb", "Metadata" ] using above senario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.