jgrammer42
asked on
How to parse JSON data object with C#?
I have a stupid question. I am relatively new to C# and I know I can do what I want to do, but just cannot figure out how. I have the following code fragment where I am pulling a JSON formatted object from a web API. I get everything from the connection cleanly and I am able to read it all into the string variable in the code fragment called json and even in the deserialized object jsonEmp.ToString().
The JSON data has the format show below the code fragment.
Here is my question: All I want to do is walk through the JSON data so that I can pull each individual row out of those two retrieved objects. I know I should be able to do a 'foreach' statement, but I cannot figure out how to do that.
Basically, here is what I want to do: I want to pull the "Id:", "Name:", and "Description:" from the returned JSON data object and assign them to string variables to display them individually. But how do I parse the object row to individual string variables?
Can someone point me in the right direction?
Thank you very much in advance,
Jeff
JSON data
The JSON data has the format show below the code fragment.
Here is my question: All I want to do is walk through the JSON data so that I can pull each individual row out of those two retrieved objects. I know I should be able to do a 'foreach' statement, but I cannot figure out how to do that.
Basically, here is what I want to do: I want to pull the "Id:", "Name:", and "Description:" from the returned JSON data object and assign them to string variables to display them individually. But how do I parse the object row to individual string variables?
Can someone point me in the right direction?
Thank you very much in advance,
Jeff
using (var reader = new StreamReader(stream))
{
string json = reader.ReadToEnd();
var jsonObj = JsonConvert.DeserializeObject(json) as JObject;
var jsonArray = jsonObj["value"] as JArray;
foreach (var jsonEmp in jsonArray)
{
var obj = JsonConvert.DeserializeObject<T>(jsonEmp.ToString());
data.Add(obj);
WriteLine("{0}", jsonEmp.ToString());
WriteLine("end of row");
}
// Following two lines need to be commented out before going into production
// They are just used for development purposes.
WriteLine("Press a key to exit...");
ReadKey(true);
}
JSON data
{
"@odata.context": "http://website.com/dataServices/api/$metadata#Location",
"value": [
{
"Id": 4,
"Name": "BE101",
"Description": "GTS Ctr"
},
{
"Id": 5,
"Name": "BE109",
"Description": "Youth Career Ctr"
},
{
"Id": 6,
"Name": "BE110",
"Description": "Youth Career Ctr"
}
]
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
JSON.NET from NuGet!
Assuming, of course, that you add a reference to ASKER
Steven,
(First, I am using Newtonsoft.JSON )
Ah, I think I understand a little better. But I still am missing something.
Using your class then would I write a foreach statement like this?
because when I add that class, and then try to reference it with that construct, Visual Studio tells me the following:
"JToken does not contain a definition for "Id, Name, or Description", and not extension method for any of them as well.
So, what am I missing?
Thank you so much for your help,
jeff
(First, I am using Newtonsoft.JSON )
Ah, I think I understand a little better. But I still am missing something.
Using your class then would I write a foreach statement like this?
foreach (var result in jsonArray)
{
WriteLine("{0} {1} {2}", result.Id,result.Name,result.Description);
WriteLine("end of row");
}
because when I add that class, and then try to reference it with that construct, Visual Studio tells me the following:
"JToken does not contain a definition for "Id, Name, or Description", and not extension method for any of them as well.
So, what am I missing?
Thank you so much for your help,
jeff
Using something like http://json2csharp.com/ , you can create a set of classes that represent your JSON structure:
Since the @odata.context is invalid as a C# identifier, you'll have to tweak that:
public class Value
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class RootObject
{
public string __invalid_name__@odata.context { get; set; }
public List<Value> value { get; set; }
}
Since the @odata.context is invalid as a C# identifier, you'll have to tweak that:
public class RootObject
{
[JsonProperty(PropertyName = "@odata.context")]
public string Context { get; set; }
public List<Value> value { get; set; }
}
ASKER
Thank you very much! You put me on the right path.