1

Web Service required this format:

{
   "Data":"{\"Name\":\"HelloWorld\",\"BirthDate\":\"2020-03-03\",\"BirthPlace\":\"Nowhere\"}"
}

I required above format to post to web service but my code below doesn't fulfil the format. Please help. I've been using below code to post

 var Data = JsonConvert.SerializeObject(new
 {
    Data = new
    {
      Name= "HelloWorld",
      BirthDate = "2020-03-03",
      BirthPlace= "Nowhere"
    }
 });
 using (var client = new HttpClient())
 {
    HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl, Data);
 }
6
  • 1
    Surely you dont need to post the escape on the quotes? (\") Commented Dec 5, 2019 at 7:08
  • Print the string you get and compare it to your required string. You'll find that you get {"Data":{"Name":"HelloWorld","BirthDate":"2020-03-03","BirthPlace":"Nowhere"}} Commented Dec 5, 2019 at 7:11
  • Unrelated: using (var client = new HttpClient()) is just for the example, right? You shouldn't use it that way in production code. Commented Dec 5, 2019 at 7:24
  • 1
    If The data property must really be a string with the serialisation of the inner object you can simply serilise it twice. var Data = Newtonsoft.Json.JsonConvert.SerializeObject( new { Data = Newtonsoft.Json.JsonConvert.SerializeObject(new { Name = "HelloWorld", BirthDate = "2020-03-03", BirthPlace = "Nowhere" }) }); Commented Dec 5, 2019 at 7:29
  • @xdtTransform You should write that up as answer. Seems reasonable to me and it is a different approach from my answer. In fact, I find it a little bit better. Commented Dec 5, 2019 at 7:59

2 Answers 2

1

If data should contains a string serialization of the real object. You can simply serialize the inner object using the string result as value on your second serialization.

using Newtonsoft.Json;

public static string WrapAndSerialize(object value){
    return JsonConvert.SerializeObject(new { Data = JsonConvert.SerializeObject(value) });  
}

Using it like:

var myObject= 
  new
  {
     Name = "HelloWorld",
     BirthDate = "2020-03-03", 
     BirthPlace = "Nowhere",
  };

var Data= WrapAndSerialize(myObject);
using (var client = new HttpClient())
{
    HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl, Data);
}

LiveDemo

Sign up to request clarification or add additional context in comments.

Comments

1

To get the required format do:

string name = "HelloWorld";
string birthdate = "2020-03-03";
string birthplace= "Nowhere";
var jsonData = JsonConvert.SerializeObject(new
{
    Data = $"\"Name\"=\"{name}\",\"BirthDate\"=\"{birthdate}\",\"BirthPlace\"=\"{birthplace}\""
});

See it in action: https://dotnetfiddle.net/UBXDtd

The format states that Data shall contain a string. Your code serializes an object with properties, which results in:

{"Data":{"Name":"HelloWorld","BirthDate":"2020-03-03","BirthPlace":"Nowhere"}}

EDIT: While this works, I would recommend @xdtTransform's answer over this. Leaving this here, in case his solution is for some reason not applicable.

Comments

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.