How to convert a JSON object containing an array to C# Dictionary?
I am sending a request to a WebAPI using following code:
client.PostAsync(baseAddress + path, new FormUrlEncodedContent(JsonConvert.DeserializeObject<Dictionary<string,string>>(form)))
where client
is an object of HttpClient
class. This code is executed for all the requests to the WebApi. I am trying to send following data to the API:
{
"code":"GUEST",
"category":"Indian",
"sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],
"date":"0001-01-01T00:00:00",
"time":"0001-01-01T00:00:00",
"quantity":1.0,
"price":0.0,
"discount":0.0,
"paymentMethod":"ID",
"paymentMethodID":null,
"ticketNo":null
}
Now because the FormUrlEncodedContent
accepts only the Dictionary<string,string>
object, I am converting this JSON into that type using NewtonSoft's JSON.NET method JsonConvert.DeserializeObject
. But at the point where sections
array starts, it is showing me this error message:
Unexpected character encountered while parsing value:[. Path 'sections'
.
So, what approach should I follow if I want to use the same code for this kind of JSON data?
c# asp.net-mvc winforms asp.net-web-api json.net
add a comment |
I am sending a request to a WebAPI using following code:
client.PostAsync(baseAddress + path, new FormUrlEncodedContent(JsonConvert.DeserializeObject<Dictionary<string,string>>(form)))
where client
is an object of HttpClient
class. This code is executed for all the requests to the WebApi. I am trying to send following data to the API:
{
"code":"GUEST",
"category":"Indian",
"sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],
"date":"0001-01-01T00:00:00",
"time":"0001-01-01T00:00:00",
"quantity":1.0,
"price":0.0,
"discount":0.0,
"paymentMethod":"ID",
"paymentMethodID":null,
"ticketNo":null
}
Now because the FormUrlEncodedContent
accepts only the Dictionary<string,string>
object, I am converting this JSON into that type using NewtonSoft's JSON.NET method JsonConvert.DeserializeObject
. But at the point where sections
array starts, it is showing me this error message:
Unexpected character encountered while parsing value:[. Path 'sections'
.
So, what approach should I follow if I want to use the same code for this kind of JSON data?
c# asp.net-mvc winforms asp.net-web-api json.net
You won't be able to send this as FormUrlEncodedContent. Can't you simply send the json as a body of your post request, not URL-encoded?
– Pac0
Nov 15 '18 at 8:59
1
@Pac0 Actually the API only accepts in this format. I tried other ways but no luck. Also, other methods are passing the data in the same way.
– Aishwarya Shiva
Nov 15 '18 at 9:14
add a comment |
I am sending a request to a WebAPI using following code:
client.PostAsync(baseAddress + path, new FormUrlEncodedContent(JsonConvert.DeserializeObject<Dictionary<string,string>>(form)))
where client
is an object of HttpClient
class. This code is executed for all the requests to the WebApi. I am trying to send following data to the API:
{
"code":"GUEST",
"category":"Indian",
"sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],
"date":"0001-01-01T00:00:00",
"time":"0001-01-01T00:00:00",
"quantity":1.0,
"price":0.0,
"discount":0.0,
"paymentMethod":"ID",
"paymentMethodID":null,
"ticketNo":null
}
Now because the FormUrlEncodedContent
accepts only the Dictionary<string,string>
object, I am converting this JSON into that type using NewtonSoft's JSON.NET method JsonConvert.DeserializeObject
. But at the point where sections
array starts, it is showing me this error message:
Unexpected character encountered while parsing value:[. Path 'sections'
.
So, what approach should I follow if I want to use the same code for this kind of JSON data?
c# asp.net-mvc winforms asp.net-web-api json.net
I am sending a request to a WebAPI using following code:
client.PostAsync(baseAddress + path, new FormUrlEncodedContent(JsonConvert.DeserializeObject<Dictionary<string,string>>(form)))
where client
is an object of HttpClient
class. This code is executed for all the requests to the WebApi. I am trying to send following data to the API:
{
"code":"GUEST",
"category":"Indian",
"sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],
"date":"0001-01-01T00:00:00",
"time":"0001-01-01T00:00:00",
"quantity":1.0,
"price":0.0,
"discount":0.0,
"paymentMethod":"ID",
"paymentMethodID":null,
"ticketNo":null
}
Now because the FormUrlEncodedContent
accepts only the Dictionary<string,string>
object, I am converting this JSON into that type using NewtonSoft's JSON.NET method JsonConvert.DeserializeObject
. But at the point where sections
array starts, it is showing me this error message:
Unexpected character encountered while parsing value:[. Path 'sections'
.
So, what approach should I follow if I want to use the same code for this kind of JSON data?
c# asp.net-mvc winforms asp.net-web-api json.net
c# asp.net-mvc winforms asp.net-web-api json.net
asked Nov 15 '18 at 8:48
Aishwarya ShivaAishwarya Shiva
1,75674084
1,75674084
You won't be able to send this as FormUrlEncodedContent. Can't you simply send the json as a body of your post request, not URL-encoded?
– Pac0
Nov 15 '18 at 8:59
1
@Pac0 Actually the API only accepts in this format. I tried other ways but no luck. Also, other methods are passing the data in the same way.
– Aishwarya Shiva
Nov 15 '18 at 9:14
add a comment |
You won't be able to send this as FormUrlEncodedContent. Can't you simply send the json as a body of your post request, not URL-encoded?
– Pac0
Nov 15 '18 at 8:59
1
@Pac0 Actually the API only accepts in this format. I tried other ways but no luck. Also, other methods are passing the data in the same way.
– Aishwarya Shiva
Nov 15 '18 at 9:14
You won't be able to send this as FormUrlEncodedContent. Can't you simply send the json as a body of your post request, not URL-encoded?
– Pac0
Nov 15 '18 at 8:59
You won't be able to send this as FormUrlEncodedContent. Can't you simply send the json as a body of your post request, not URL-encoded?
– Pac0
Nov 15 '18 at 8:59
1
1
@Pac0 Actually the API only accepts in this format. I tried other ways but no luck. Also, other methods are passing the data in the same way.
– Aishwarya Shiva
Nov 15 '18 at 9:14
@Pac0 Actually the API only accepts in this format. I tried other ways but no luck. Also, other methods are passing the data in the same way.
– Aishwarya Shiva
Nov 15 '18 at 9:14
add a comment |
1 Answer
1
active
oldest
votes
If you for any reason need to send all values as strings, then you must convert array of strings to string before deserializing it to Dictionary<string, string>
.
It can be done like this:
var json = "{"code":"GUEST","category":"Indian","sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],"date":"0001-01-01T00:00:00","time":"0001-01-01T00:00:00","quantity":1.0,"price":0.0,"discount":0.0,"paymentMethod":"ID","paymentMethodID":null,"ticketNo":null}";
var jObject = JObject.Parse(json);
jObject["sections"] = JsonConvert.SerializeObject(jObject["sections"].ToObject<string>());
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject.ToString());
That way you will get result:
Thanks :) This worked. Although I modified the logic to incorporate this. But it's working fine now.
– Aishwarya Shiva
Nov 15 '18 at 13:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315488%2fhow-to-convert-a-json-object-containing-an-array-to-c-sharp-dictionarystring-st%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you for any reason need to send all values as strings, then you must convert array of strings to string before deserializing it to Dictionary<string, string>
.
It can be done like this:
var json = "{"code":"GUEST","category":"Indian","sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],"date":"0001-01-01T00:00:00","time":"0001-01-01T00:00:00","quantity":1.0,"price":0.0,"discount":0.0,"paymentMethod":"ID","paymentMethodID":null,"ticketNo":null}";
var jObject = JObject.Parse(json);
jObject["sections"] = JsonConvert.SerializeObject(jObject["sections"].ToObject<string>());
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject.ToString());
That way you will get result:
Thanks :) This worked. Although I modified the logic to incorporate this. But it's working fine now.
– Aishwarya Shiva
Nov 15 '18 at 13:10
add a comment |
If you for any reason need to send all values as strings, then you must convert array of strings to string before deserializing it to Dictionary<string, string>
.
It can be done like this:
var json = "{"code":"GUEST","category":"Indian","sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],"date":"0001-01-01T00:00:00","time":"0001-01-01T00:00:00","quantity":1.0,"price":0.0,"discount":0.0,"paymentMethod":"ID","paymentMethodID":null,"ticketNo":null}";
var jObject = JObject.Parse(json);
jObject["sections"] = JsonConvert.SerializeObject(jObject["sections"].ToObject<string>());
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject.ToString());
That way you will get result:
Thanks :) This worked. Although I modified the logic to incorporate this. But it's working fine now.
– Aishwarya Shiva
Nov 15 '18 at 13:10
add a comment |
If you for any reason need to send all values as strings, then you must convert array of strings to string before deserializing it to Dictionary<string, string>
.
It can be done like this:
var json = "{"code":"GUEST","category":"Indian","sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],"date":"0001-01-01T00:00:00","time":"0001-01-01T00:00:00","quantity":1.0,"price":0.0,"discount":0.0,"paymentMethod":"ID","paymentMethodID":null,"ticketNo":null}";
var jObject = JObject.Parse(json);
jObject["sections"] = JsonConvert.SerializeObject(jObject["sections"].ToObject<string>());
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject.ToString());
That way you will get result:
If you for any reason need to send all values as strings, then you must convert array of strings to string before deserializing it to Dictionary<string, string>
.
It can be done like this:
var json = "{"code":"GUEST","category":"Indian","sections":["01000000-0000-0000-0000-000000000000","02000000-0000-0000-0000-000000000000"],"date":"0001-01-01T00:00:00","time":"0001-01-01T00:00:00","quantity":1.0,"price":0.0,"discount":0.0,"paymentMethod":"ID","paymentMethodID":null,"ticketNo":null}";
var jObject = JObject.Parse(json);
jObject["sections"] = JsonConvert.SerializeObject(jObject["sections"].ToObject<string>());
var result = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject.ToString());
That way you will get result:
edited Nov 15 '18 at 9:49
answered Nov 15 '18 at 9:13
Tomas ChabadaTomas Chabada
1,18379
1,18379
Thanks :) This worked. Although I modified the logic to incorporate this. But it's working fine now.
– Aishwarya Shiva
Nov 15 '18 at 13:10
add a comment |
Thanks :) This worked. Although I modified the logic to incorporate this. But it's working fine now.
– Aishwarya Shiva
Nov 15 '18 at 13:10
Thanks :) This worked. Although I modified the logic to incorporate this. But it's working fine now.
– Aishwarya Shiva
Nov 15 '18 at 13:10
Thanks :) This worked. Although I modified the logic to incorporate this. But it's working fine now.
– Aishwarya Shiva
Nov 15 '18 at 13:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315488%2fhow-to-convert-a-json-object-containing-an-array-to-c-sharp-dictionarystring-st%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You won't be able to send this as FormUrlEncodedContent. Can't you simply send the json as a body of your post request, not URL-encoded?
– Pac0
Nov 15 '18 at 8:59
1
@Pac0 Actually the API only accepts in this format. I tried other ways but no luck. Also, other methods are passing the data in the same way.
– Aishwarya Shiva
Nov 15 '18 at 9:14