simple json retrieval
I know this is an old chestnut but I want to do this without importing newton-soft or json.net
I know this should work
this is the json :
{ "do": "Thing", "with": "abc" }
literally that's it. I need to get this into c# land
this is what I have so far
var json = wc.DownloadString("url");
Console.WriteLine("GOT >> " + json); //says GOT >> { "do": "Thing", "with": "abc" }
var sJson = new JavaScriptSerializer();
var data = sJson.Deserialize<Dictionary<string, string>>(json); //crashes with No parameterless constructor defined for type of 'System.Collections.Generic.Dictionary
what is the leanest least bloated way possible way that I can get data["do"]
and data["with"]
from my single line json? It will only ever return one thing ... if I have to string walk it I will but it shouldn't be this hard
c# json dictionary
add a comment |
I know this is an old chestnut but I want to do this without importing newton-soft or json.net
I know this should work
this is the json :
{ "do": "Thing", "with": "abc" }
literally that's it. I need to get this into c# land
this is what I have so far
var json = wc.DownloadString("url");
Console.WriteLine("GOT >> " + json); //says GOT >> { "do": "Thing", "with": "abc" }
var sJson = new JavaScriptSerializer();
var data = sJson.Deserialize<Dictionary<string, string>>(json); //crashes with No parameterless constructor defined for type of 'System.Collections.Generic.Dictionary
what is the leanest least bloated way possible way that I can get data["do"]
and data["with"]
from my single line json? It will only ever return one thing ... if I have to string walk it I will but it shouldn't be this hard
c# json dictionary
1
WhyDictionary<string, string>
array? What you have is aDictionary<string, string>
at best
– Nkosi
Dec 31 '18 at 12:44
because "do" and "with", but come to think of it I don't need that, originally there was a array in there for with and I chopped it up a bit
– Mr Heelis
Dec 31 '18 at 12:46
I did it like this in the endDo data = sJson.Deserialize<Do>(json);
whereDo
was simplypublic string todo = ""; public string with = "";
still seems highly sub-optimal but hey ho
– Mr Heelis
Dec 31 '18 at 12:49
1
"do" and "with" what? :) There's no array in your sample JSON, so it corresponds toDictionary<string, string>
, "do" and "with" being the keys and "Thing" and "abc" being their corresponding values.
– Szab
Dec 31 '18 at 12:49
@MrHeelis check provided answer
– Nkosi
Dec 31 '18 at 13:40
add a comment |
I know this is an old chestnut but I want to do this without importing newton-soft or json.net
I know this should work
this is the json :
{ "do": "Thing", "with": "abc" }
literally that's it. I need to get this into c# land
this is what I have so far
var json = wc.DownloadString("url");
Console.WriteLine("GOT >> " + json); //says GOT >> { "do": "Thing", "with": "abc" }
var sJson = new JavaScriptSerializer();
var data = sJson.Deserialize<Dictionary<string, string>>(json); //crashes with No parameterless constructor defined for type of 'System.Collections.Generic.Dictionary
what is the leanest least bloated way possible way that I can get data["do"]
and data["with"]
from my single line json? It will only ever return one thing ... if I have to string walk it I will but it shouldn't be this hard
c# json dictionary
I know this is an old chestnut but I want to do this without importing newton-soft or json.net
I know this should work
this is the json :
{ "do": "Thing", "with": "abc" }
literally that's it. I need to get this into c# land
this is what I have so far
var json = wc.DownloadString("url");
Console.WriteLine("GOT >> " + json); //says GOT >> { "do": "Thing", "with": "abc" }
var sJson = new JavaScriptSerializer();
var data = sJson.Deserialize<Dictionary<string, string>>(json); //crashes with No parameterless constructor defined for type of 'System.Collections.Generic.Dictionary
what is the leanest least bloated way possible way that I can get data["do"]
and data["with"]
from my single line json? It will only ever return one thing ... if I have to string walk it I will but it shouldn't be this hard
c# json dictionary
c# json dictionary
edited Jan 2 at 8:41
aloisdg
9,58224156
9,58224156
asked Dec 31 '18 at 12:33
Mr HeelisMr Heelis
1,04111221
1,04111221
1
WhyDictionary<string, string>
array? What you have is aDictionary<string, string>
at best
– Nkosi
Dec 31 '18 at 12:44
because "do" and "with", but come to think of it I don't need that, originally there was a array in there for with and I chopped it up a bit
– Mr Heelis
Dec 31 '18 at 12:46
I did it like this in the endDo data = sJson.Deserialize<Do>(json);
whereDo
was simplypublic string todo = ""; public string with = "";
still seems highly sub-optimal but hey ho
– Mr Heelis
Dec 31 '18 at 12:49
1
"do" and "with" what? :) There's no array in your sample JSON, so it corresponds toDictionary<string, string>
, "do" and "with" being the keys and "Thing" and "abc" being their corresponding values.
– Szab
Dec 31 '18 at 12:49
@MrHeelis check provided answer
– Nkosi
Dec 31 '18 at 13:40
add a comment |
1
WhyDictionary<string, string>
array? What you have is aDictionary<string, string>
at best
– Nkosi
Dec 31 '18 at 12:44
because "do" and "with", but come to think of it I don't need that, originally there was a array in there for with and I chopped it up a bit
– Mr Heelis
Dec 31 '18 at 12:46
I did it like this in the endDo data = sJson.Deserialize<Do>(json);
whereDo
was simplypublic string todo = ""; public string with = "";
still seems highly sub-optimal but hey ho
– Mr Heelis
Dec 31 '18 at 12:49
1
"do" and "with" what? :) There's no array in your sample JSON, so it corresponds toDictionary<string, string>
, "do" and "with" being the keys and "Thing" and "abc" being their corresponding values.
– Szab
Dec 31 '18 at 12:49
@MrHeelis check provided answer
– Nkosi
Dec 31 '18 at 13:40
1
1
Why
Dictionary<string, string>
array? What you have is a Dictionary<string, string>
at best– Nkosi
Dec 31 '18 at 12:44
Why
Dictionary<string, string>
array? What you have is a Dictionary<string, string>
at best– Nkosi
Dec 31 '18 at 12:44
because "do" and "with", but come to think of it I don't need that, originally there was a array in there for with and I chopped it up a bit
– Mr Heelis
Dec 31 '18 at 12:46
because "do" and "with", but come to think of it I don't need that, originally there was a array in there for with and I chopped it up a bit
– Mr Heelis
Dec 31 '18 at 12:46
I did it like this in the end
Do data = sJson.Deserialize<Do>(json);
where Do
was simply public string todo = ""; public string with = "";
still seems highly sub-optimal but hey ho– Mr Heelis
Dec 31 '18 at 12:49
I did it like this in the end
Do data = sJson.Deserialize<Do>(json);
where Do
was simply public string todo = ""; public string with = "";
still seems highly sub-optimal but hey ho– Mr Heelis
Dec 31 '18 at 12:49
1
1
"do" and "with" what? :) There's no array in your sample JSON, so it corresponds to
Dictionary<string, string>
, "do" and "with" being the keys and "Thing" and "abc" being their corresponding values.– Szab
Dec 31 '18 at 12:49
"do" and "with" what? :) There's no array in your sample JSON, so it corresponds to
Dictionary<string, string>
, "do" and "with" being the keys and "Thing" and "abc" being their corresponding values.– Szab
Dec 31 '18 at 12:49
@MrHeelis check provided answer
– Nkosi
Dec 31 '18 at 13:40
@MrHeelis check provided answer
– Nkosi
Dec 31 '18 at 13:40
add a comment |
4 Answers
4
active
oldest
votes
You can create a backing class for the data
public class Data {
public string do { get; set; }
public string with { get; set; }
}
and simply desrialize to that
var data = sJson.Deserialize<Data>(json);
If the provided data is in fact an array then update the generic return type accordingly
add a comment |
There's no array in your JSON, just a simple object so it can be deserialized to a single Dictionary
instance. Simply change Dictionary<string, string>
to Dictionary<string, string>
. Like this:
var data = sJson.Deserialize<Dictionary<string, string>>(json);
You can then access your values like this:
data["do"] // returns "Thing"
data["with"] // returns "abc"
add a comment |
The array is the problem. Try this (Try it Online!):
var json = "{ "do": "Thing", "with": "abc" }";
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
Console.WriteLine(data["do"]);
Console.WriteLine(data["with"]);
output
Thing
abc
Note that I am using Json.NET here as written in the documentation:
Json.NET should be used for serialization and deserialization. Provides serialization and deserialization functionality for AJAX-enabled applications.
source
2
The author wrote at the beginning of his question: I want to do this without importing newton-soft or json.net ;)
– Szab
Dec 31 '18 at 13:12
Oh I missed it.
– aloisdg
Jan 1 at 13:41
add a comment |
You can use Regex :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string args)
{
string input = "{ "do": "Thing", "with": "abc" }";
string pattern = ""(?'key'[^"]+)":\s+"(?'value'[^"]+)";
MatchCollection matches = Regex.Matches(input, pattern);
Dictionary<string, string> dict = matches.Cast<Match>()
.GroupBy(x => x.Groups["key"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
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%2f53987576%2fsimple-json-retrieval%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can create a backing class for the data
public class Data {
public string do { get; set; }
public string with { get; set; }
}
and simply desrialize to that
var data = sJson.Deserialize<Data>(json);
If the provided data is in fact an array then update the generic return type accordingly
add a comment |
You can create a backing class for the data
public class Data {
public string do { get; set; }
public string with { get; set; }
}
and simply desrialize to that
var data = sJson.Deserialize<Data>(json);
If the provided data is in fact an array then update the generic return type accordingly
add a comment |
You can create a backing class for the data
public class Data {
public string do { get; set; }
public string with { get; set; }
}
and simply desrialize to that
var data = sJson.Deserialize<Data>(json);
If the provided data is in fact an array then update the generic return type accordingly
You can create a backing class for the data
public class Data {
public string do { get; set; }
public string with { get; set; }
}
and simply desrialize to that
var data = sJson.Deserialize<Data>(json);
If the provided data is in fact an array then update the generic return type accordingly
edited Dec 31 '18 at 18:49
answered Dec 31 '18 at 13:38
NkosiNkosi
115k16127192
115k16127192
add a comment |
add a comment |
There's no array in your JSON, just a simple object so it can be deserialized to a single Dictionary
instance. Simply change Dictionary<string, string>
to Dictionary<string, string>
. Like this:
var data = sJson.Deserialize<Dictionary<string, string>>(json);
You can then access your values like this:
data["do"] // returns "Thing"
data["with"] // returns "abc"
add a comment |
There's no array in your JSON, just a simple object so it can be deserialized to a single Dictionary
instance. Simply change Dictionary<string, string>
to Dictionary<string, string>
. Like this:
var data = sJson.Deserialize<Dictionary<string, string>>(json);
You can then access your values like this:
data["do"] // returns "Thing"
data["with"] // returns "abc"
add a comment |
There's no array in your JSON, just a simple object so it can be deserialized to a single Dictionary
instance. Simply change Dictionary<string, string>
to Dictionary<string, string>
. Like this:
var data = sJson.Deserialize<Dictionary<string, string>>(json);
You can then access your values like this:
data["do"] // returns "Thing"
data["with"] // returns "abc"
There's no array in your JSON, just a simple object so it can be deserialized to a single Dictionary
instance. Simply change Dictionary<string, string>
to Dictionary<string, string>
. Like this:
var data = sJson.Deserialize<Dictionary<string, string>>(json);
You can then access your values like this:
data["do"] // returns "Thing"
data["with"] // returns "abc"
answered Dec 31 '18 at 12:56
SzabSzab
1,053517
1,053517
add a comment |
add a comment |
The array is the problem. Try this (Try it Online!):
var json = "{ "do": "Thing", "with": "abc" }";
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
Console.WriteLine(data["do"]);
Console.WriteLine(data["with"]);
output
Thing
abc
Note that I am using Json.NET here as written in the documentation:
Json.NET should be used for serialization and deserialization. Provides serialization and deserialization functionality for AJAX-enabled applications.
source
2
The author wrote at the beginning of his question: I want to do this without importing newton-soft or json.net ;)
– Szab
Dec 31 '18 at 13:12
Oh I missed it.
– aloisdg
Jan 1 at 13:41
add a comment |
The array is the problem. Try this (Try it Online!):
var json = "{ "do": "Thing", "with": "abc" }";
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
Console.WriteLine(data["do"]);
Console.WriteLine(data["with"]);
output
Thing
abc
Note that I am using Json.NET here as written in the documentation:
Json.NET should be used for serialization and deserialization. Provides serialization and deserialization functionality for AJAX-enabled applications.
source
2
The author wrote at the beginning of his question: I want to do this without importing newton-soft or json.net ;)
– Szab
Dec 31 '18 at 13:12
Oh I missed it.
– aloisdg
Jan 1 at 13:41
add a comment |
The array is the problem. Try this (Try it Online!):
var json = "{ "do": "Thing", "with": "abc" }";
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
Console.WriteLine(data["do"]);
Console.WriteLine(data["with"]);
output
Thing
abc
Note that I am using Json.NET here as written in the documentation:
Json.NET should be used for serialization and deserialization. Provides serialization and deserialization functionality for AJAX-enabled applications.
source
The array is the problem. Try this (Try it Online!):
var json = "{ "do": "Thing", "with": "abc" }";
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
Console.WriteLine(data["do"]);
Console.WriteLine(data["with"]);
output
Thing
abc
Note that I am using Json.NET here as written in the documentation:
Json.NET should be used for serialization and deserialization. Provides serialization and deserialization functionality for AJAX-enabled applications.
source
answered Dec 31 '18 at 13:03
aloisdgaloisdg
9,58224156
9,58224156
2
The author wrote at the beginning of his question: I want to do this without importing newton-soft or json.net ;)
– Szab
Dec 31 '18 at 13:12
Oh I missed it.
– aloisdg
Jan 1 at 13:41
add a comment |
2
The author wrote at the beginning of his question: I want to do this without importing newton-soft or json.net ;)
– Szab
Dec 31 '18 at 13:12
Oh I missed it.
– aloisdg
Jan 1 at 13:41
2
2
The author wrote at the beginning of his question: I want to do this without importing newton-soft or json.net ;)
– Szab
Dec 31 '18 at 13:12
The author wrote at the beginning of his question: I want to do this without importing newton-soft or json.net ;)
– Szab
Dec 31 '18 at 13:12
Oh I missed it.
– aloisdg
Jan 1 at 13:41
Oh I missed it.
– aloisdg
Jan 1 at 13:41
add a comment |
You can use Regex :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string args)
{
string input = "{ "do": "Thing", "with": "abc" }";
string pattern = ""(?'key'[^"]+)":\s+"(?'value'[^"]+)";
MatchCollection matches = Regex.Matches(input, pattern);
Dictionary<string, string> dict = matches.Cast<Match>()
.GroupBy(x => x.Groups["key"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
add a comment |
You can use Regex :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string args)
{
string input = "{ "do": "Thing", "with": "abc" }";
string pattern = ""(?'key'[^"]+)":\s+"(?'value'[^"]+)";
MatchCollection matches = Regex.Matches(input, pattern);
Dictionary<string, string> dict = matches.Cast<Match>()
.GroupBy(x => x.Groups["key"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
add a comment |
You can use Regex :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string args)
{
string input = "{ "do": "Thing", "with": "abc" }";
string pattern = ""(?'key'[^"]+)":\s+"(?'value'[^"]+)";
MatchCollection matches = Regex.Matches(input, pattern);
Dictionary<string, string> dict = matches.Cast<Match>()
.GroupBy(x => x.Groups["key"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
You can use Regex :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string args)
{
string input = "{ "do": "Thing", "with": "abc" }";
string pattern = ""(?'key'[^"]+)":\s+"(?'value'[^"]+)";
MatchCollection matches = Regex.Matches(input, pattern);
Dictionary<string, string> dict = matches.Cast<Match>()
.GroupBy(x => x.Groups["key"].Value, y => y.Groups["value"].Value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
}
answered Dec 31 '18 at 13:23
jdwengjdweng
17.4k2717
17.4k2717
add a comment |
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%2f53987576%2fsimple-json-retrieval%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
1
Why
Dictionary<string, string>
array? What you have is aDictionary<string, string>
at best– Nkosi
Dec 31 '18 at 12:44
because "do" and "with", but come to think of it I don't need that, originally there was a array in there for with and I chopped it up a bit
– Mr Heelis
Dec 31 '18 at 12:46
I did it like this in the end
Do data = sJson.Deserialize<Do>(json);
whereDo
was simplypublic string todo = ""; public string with = "";
still seems highly sub-optimal but hey ho– Mr Heelis
Dec 31 '18 at 12:49
1
"do" and "with" what? :) There's no array in your sample JSON, so it corresponds to
Dictionary<string, string>
, "do" and "with" being the keys and "Thing" and "abc" being their corresponding values.– Szab
Dec 31 '18 at 12:49
@MrHeelis check provided answer
– Nkosi
Dec 31 '18 at 13:40