-1

Im in trouble, how i can write post data like this(code below) with Content-Type: application/json in C#?

{
  "snippet": {
    "data": "t1",
    "data2": "t2",
    "data3": "t3",
    "data4": "t4"
  },
  "data": {
    "st": "bxx"
  }
}

Damn guys, i can't undestand how i can use JSON Serialization with my code:( Help pls little girl:D Im a newbuy. Put please { "snippet": { "data": "t1", "data2": "t2", "data3": "t3", "data4": "t4" }, "data": { "st": "bxx" } }

with JSON Serialization in my code:

var resultHttpPost = ZK.HttpPost("https://urlcom", "here is need be code with json", "application/json", "", "iso-8859-1", ZK.InterfacesLibrary.Z.Http.ResponceType.HeaderAndBody);

I just can't understand how i merge it and put right escapes in json code:(

{ "snippet\": { "data\": "t1", "data2\": "t2", "data3\": "t3", "data4\": "t4" }, "data\": { "st\": "bxx" } } Doesnt worked:(

JaneFarrow
  • 101
  • 2
  • 10
  • 1
    Q: How can I post (JSON) data? A: You can google "C# JSON", and find links like this: [JSON Serialization](https://msdn.microsoft.com/en-us/library/bb410770%28v=vs.110%29.aspx). Q: How can I post multi-line JSON data? A: The format is irrelevant. As far as the HTTP message - it doesn't matter if it's one line, or multiple lines,. – FoggyDay Feb 14 '15 at 01:08
  • lots or working example can be located here.. good luck [C# Working Examples JSON](http://www.google.com) – MethodMan Feb 14 '15 at 01:09
  • I was googling it, its doesnt heping me.. In example what link you give me there is json in one line({“age”:42,”name”:”John”}) but i have multiply lines. I need make post request with separated by newline – JaneFarrow Feb 14 '15 at 01:11
  • 2
    Why do you think you need to make the request separated by newline? It is usually just printed that way for readability. – dub stylee Feb 14 '15 at 01:12

2 Answers2

0

You've really got three separate questions here:

Q1) How can I post (JSON) data?

A: You can google "C# JSON", and find links like this: JSON Serialization.

Q2) How can I post multi-line JSON data?

A: The format is irrelevant. As far as the HTTP message - it doesn't matter if it's one line, or multiple lines. "Formatting" is a presentation issue - JSON parsers will read exactly the same message with or without formatting.

If you want to present in a "fomatted" form, then:

a) you must not send as "Content-Type: application/json". You should use "text/plain" or "html/text" instead

... and ...

b) you should use the HTML <pre> tag to preserve formatting.

Q3) How do I set "Content-Type"?

That depends entirely on what library you're using to create and send your HTTP data.

Here is one possibility:

How do you set the Content-Type header for an HttpClient request?

HttpClient c = new HttpClient();
c.BaseAddress = new Uri("http://example.com/");
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
...
Community
  • 1
  • 1
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
0

JSON should be in single quotes and you will need "+" operator for concatenation. Your JSON should look like this.

"{"+
  "'snippet': {"+
    "'data': 't1',"+
    "'data2': 't2',"+
    "'data3': 't3',"+
    "'data4': 't4'"+
  "},"+
  "'data': {"+
    "'st': bxx'"+
  "}"+
"}";
Vish
  • 81
  • 8