simple json retrieval












1















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










share|improve this question




















  • 1





    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













  • 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





    "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
















1















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










share|improve this question




















  • 1





    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













  • 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





    "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














1












1








1


0






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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





    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













  • 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





    "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














  • 1





    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













  • 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





    "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








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












4 Answers
4






active

oldest

votes


















3














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






share|improve this answer

































    3














    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"





    share|improve this answer































      0














      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






      share|improve this answer



















      • 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



















      -1














      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());
      }
      }
      }





      share|improve this answer























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        3














        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






        share|improve this answer






























          3














          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






          share|improve this answer




























            3












            3








            3







            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






            share|improve this answer















            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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 31 '18 at 18:49

























            answered Dec 31 '18 at 13:38









            NkosiNkosi

            115k16127192




            115k16127192

























                3














                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"





                share|improve this answer




























                  3














                  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"





                  share|improve this answer


























                    3












                    3








                    3







                    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"





                    share|improve this answer













                    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"






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 31 '18 at 12:56









                    SzabSzab

                    1,053517




                    1,053517























                        0














                        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






                        share|improve this answer



















                        • 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
















                        0














                        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






                        share|improve this answer



















                        • 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














                        0












                        0








                        0







                        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






                        share|improve this answer













                        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







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        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














                        • 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











                        -1














                        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());
                        }
                        }
                        }





                        share|improve this answer




























                          -1














                          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());
                          }
                          }
                          }





                          share|improve this answer


























                            -1












                            -1








                            -1







                            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());
                            }
                            }
                            }





                            share|improve this answer













                            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());
                            }
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 31 '18 at 13:23









                            jdwengjdweng

                            17.4k2717




                            17.4k2717






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas

                                Can't read property showImagePicker of undefined in react native iOS