POST json dictionary
Due to the way JsonValueProviderFactory is implemented binding dictionaries is not supported.
POST Dictionary as JSON
Dim dict As New Dictionary(Of String, String)
dict.Add("Drinks", "2")
dict.Add("ID", "1")
Dim parameters As String = JsonConvert.SerializeObject(dict, Formatting.None)
Dim Uri As New Uri(String.Format("http://localhost:60627/home/test/"))
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim reqString() As Byte
webClient.Headers("content-type") = "application/json"
Dim senddata As Object = JsonConvert.SerializeObject(New With {Key .param = parameters}).ToString()
reqString = Encoding.Default.GetBytes(senddata)
resByte = webClient.UploadData(Uri, "post", reqString)
resString = Encoding.Default.GetString(resByte)
First change this type of Concatination
"{""Pram"":""" + parameters + """}"
for It's Make Little confustion.
And The Data Not Sending To the Server Property Because problem is Escapse Sequence That Concat is Not Given Proper Serialization of the Data.
In Above I Change the Code For
Dim parameters As String = JsonConvert.SerializeObject(dict, Formatting.None)
It's Change the Data
{"Drinks":"2","ID":"1"}
The Next Serialization It's Change the
{"param":"{\"Drinks\":\"2\",\"ID\":\"1\"}"}
But Your Serlization of Send Data is
{"param":"{"Drinks":"2","ID":"1"}"}
So, the Data is Not Sending Properly
I was Checked Properly it's Working Fine....
Post Json Dictionary to the controller
Instead of receiving the Class object, you should receive it as string and then serialize into object like below.
public JsonResult Tflow(string basedetails)
{
//some code
var model = new JavascriptSerializer().Deserialize<JsonFileContentInputs>(basedetails);
// Your code
}
How can I deserialize JSON to a simple Dictionarystring,string in ASP.NET?
Json.NET does this...
string json = @"{""key1"":""value1"",""key2"":""value2""}";
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
More examples: Serializing Collections with Json.NET
WebAPI - Posting to dictionary with json
The default JsonFormatter
is unable to bind Dictionary
from Javascript Array
because it doesn't define a key to each item.
You need to use an Object
instead:
"HandleBars": {
"something": "value"
}
How to send a json dictionary as a field in a Multipart Form API Requests call?
You should convert python dict to JSON string representation using json.dumps(payload_data)
instead of concatenating "{"
and "}"
since the latter might be error-prone.
Related Topics
C# Entity-Framework: How to Combine a .Find and .Include on a Model Object
Displayname Attribute VS Display Attribute
Determine If Current Application Is Activated (Has Focus)
How to Know User Has Clicked "X" or the "Close" Button
C# Filestream:Optimal Buffer Size for Writing Large Files
How to Use Interface as a C# Generic Type Constraint
Meanings of Declaring, Instantiating, Initializing and Assigning an Object
How to Use C# to Sanitize Input on an HTML Page
How to Solve Operator '!=' Cannot Be Applied to Operands of Type 'T' and 'T'
How to Insert a Data Table into SQL Server Database Table
Nice & Universal Way to Convert List of Items to Tree
How to Transfer Authentication from Webbrowser to Webrequest
Implementing Audit Log/Change History with MVC & Entity Framework
C# How to Test a File Is a Jpeg
Why Is Typedreference Behind the Scenes? It's So Fast and Safe... Almost Magical!