Hey i want to parse a String to a Jason Object in C#.
I have found this code but i alltimes get the problem that it means for JObject: "can't access due to protection level c#" Its because JObject is Internal in this Framework i think. But i found online so much solution and they tell all to make it like below. I dont know what i make wrong.
string APIUrlRandomNameGenerator = "https://api.parser.name/api";
var json = new WebClient().DownloadString(APIUrlRandomNameGenerator);
var jobj = new JObject();
dynamic data = JObject.Parse(json);
Console.WriteLine(data.id);
Console.WriteLine(data.first_name);
Console.WriteLine(data.last_name);
Console.WriteLine(data.gender);
CodePudding user response:
I found the solution.
I was only using the using Newtonsoft.Json.Linq; and i was able to look for the definitions but i it was needed to install the Newtonsoft Paket.
CodePudding user response:
You don'nt need to install any extra packages. Try this
var jsonParsed = JObject.Parse(json);
Console.WriteLine(jsonParsed["id"]);
Console.WriteLine(jsonParsed["first_name"]);
Console.WriteLine(jsonParsed["last_name"]);
Console.WriteLine(jsonParsed["gender"]);