2

I've found two different answers to this question - one applying JsonConvert (including JObject, JArray, JToken) and another working with JavaScriptSerializer. Both are getting a bit dated (especially the latter) and since I just learned that e.g. WebClient (which was suggested by many) is outdated by HttpClient, I'm not sure which to use (or if there's an even newer and preferred way.

I don't get any hints on these with my intellisense, neither, so I'm suspecting that they're not so widely appropriate.

I have a string that contains JSON formatted data. How does one get it into something workable in C#?

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

3

If I understand what you're asking, the short answer is Json.NET, with code samples here:

http://james.newtonking.com/json/help/index.html

var json = "{email:'bob@gmail.com'}";
var user = JsonConvert.DeserializeObject<User>(json);

Is that what you're looking for?

Ken Smith
  • 20,305
  • 15
  • 100
  • 147
  • Not fully sure. In your example it seems like I need to know the type of the object (*User*). Perhaps it's not such a big problem. However, something that is a problem is that it's half past one in the morning in Sweden and my brains is flipping me a bird - where/how do I download the assemblies for the library that you suggested? There's no "download" there. Or am I just too tired?! I've tried with "json" and "newtonsoft" in VS's manager... – Konrad Viltersten Nov 13 '14 at 00:24
  • 2
    @KonradViltersten The same way you obtain most popular libraries, via [NuGet](https://www.nuget.org/packages/Newtonsoft.Json/). And yes, with the `DeserializeObject()` method you need to have a type definition ahead of time. But Json.NET has support for situations even when you don't. Just look through their documentation. – mason Nov 13 '14 at 00:33
  • @mason I get nada hits when I try to manage the packages after right-mousing the references menu... It's supposed to look filled, like [this](http://docs.nuget.org/docs/start-here/managing-nuget-packages-using-the-dialog)... Maybe I'm just too stupid to use internet today, hehe. Or too tired... – Konrad Viltersten Nov 13 '14 at 00:34
  • 1
    @KonradViltersten Read the directions. Also if you're tired. Just go to bed. It can wait until morning. You'll avoid asking dumb questions that you'd probably be able to easily find the answer to when you're well rested. – mason Nov 13 '14 at 00:35
  • @mason No argue there. Too stubborn for my own good. Gonna hug the pillows now... – Konrad Viltersten Nov 13 '14 at 00:38
  • 1
    Yep, get some sleep. And in the morning get nuget console and go like `Install-Package Json.Net`. And it should be like `man, here you go, totally installing the package for you`. And you should go like `maaaannn, that was silly, I should really listen to strangers online, when they tell me to go to bed` -) – trailmax Nov 13 '14 at 00:38
  • @trailmax Hehe, it's *Install-Package Newtonsoft.Json*, apparently, as shown [on the Internet](https://www.nuget.org/packages/Newtonsoft.Json/). Still, I was expecting it to show in my gallery or something (plus I spelled "Json" with a "q", yesterday - no idea how). – Konrad Viltersten Nov 13 '14 at 05:03
  • @KonradViltersten duh to myself! I'm so not used to install Json.Net myself - I don't know the package name.All my other dependencies depend on it - it is installed automatically for me. Probably I should also get more sleep -) – trailmax Nov 13 '14 at 10:19