NewtonSoft Json retrieve data with brackets method - c#

Currently I am using the YouTube API to get some data. The data is in json and I am trying to get a single part of it.
The problem that I am facing right now is that I want to use the following method:
WebClient client = new WebClient();
string jsonData = client.DownloadString("https://www.googleapis.com/youtube/v3/videos?id=" + videoId + "&part=contentDetails&key=");
var results = JsonConvert.DeserializeObject(jsonData);
string duration = results["items"]["contentDetails"]["duration"];
Currently this results in an error:
Cannot apply indexing with [] to an expression of type 'object'
The problem I am facing right now is that I don't know how to deserialize the object properly so I can use text indexes to get to the data. I used to do it like this before but I can't recall it and can't find anything about my method.

What you should do is define a class with all your properties that matches your JSON, and then pass that class as a type argument to the deserialization:
Results results = JsonConvert.DeserializeObject<Results>(jsonData);
I don't have more info about your JSON, but a quick definition would look like this:
public class Results
{
public Items items { get; set; }
}
public class Items
{
public ContentDetails contentDetails { get; set; }
}
public class ContentDetails
{
public string duration { get; set; }
}
If you need help converting a JSON response to C# classes, you can use converters such as http://json2csharp.com/
You can also deserialize to a dynamic type, like so:
var results = JsonConvert.DeserializeObject<dynamic>(jsonData);
Then, you just access your properties:
string duration = results.items.contentDetails.duration;
However, using dynamic is not recommended most of the time, because it effectively removes static typing, so you must be sure that duration will be a string, or it will throw a runtime exception when you try to access it.

Normally if I'm dealing with a JSON payload that I don't feel like mapping to a strong type I use JObjects which allow for LINQ to JSON.
var jObject = JObject.Parse(jsonString);
This will allow the properties of the object to be indexed using [] syntax.
More information on using LINQ to JSON in the API docs.

Get your Deserialized data into dynamic type:
dynamic results = JsonConvert.DeserializeObject(jsonData);
and then access it using .:
string duration = results.items.contentDetails.duration;
The syntax you're using is used in javascript.

Try this way:
dynamic results = JsonConvert.DeserializeObject(jsonData);
then you can do
string duration = results["items"]["contentDetails"]["duration"];
or
string duration = results.items.contentDetails.duration;
Reference to dynamic keyword on MSDN: https://msdn.microsoft.com/en-us/library/dd264736.aspx

Related

Deserialize objects from array nested inside Json response

I'm trying to deserialize some objects nested inside a Json response using Newtonsoft.Json. I want to deserialize the following Jsons Term objects into a list. I have many Term objects in the Json response, so performance and compactness is important to me. I also would only like to define the Term class as I do not care about the other data for the time being.
I have a model defined for Term:
public class Term
{
public string Known { get; set; }
public string Word { get; set; }
}
My Json looks like this:
{
"myName":"Chris",
"mySpecies":"Cat",
"myTerms":
[
{
"Term":
{
"Known":"true",
"Word":"Meow"
}
},
{
"Term":
{
"Known":"false",
"Word":"Bark"
}
}
]
}
My C# deserializing code:
var response = await httpClient.GetAsync(uri);
string responseString = response.Content.ReadAsStringAsync().GetResults();
var searchTermList = JsonConvert.DeserializeObject<List<Term>>(responseString);
The problem/error I'm receiving is, not sure how I can get these terms from the json response:
{Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current
JSON object (e.g. {"name":"value"}) into type
'System.Collections.Generic.List`1[CoreProject.Models.Term]' because
the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or
change the deserialized type so that it is a normal .NET type (e.g. not a
primitive type like integer, not a collection type like an array or List<T>)
that can be deserialized from a JSON object. JsonObjectAttribute can also be
added to the type to force it to deserialize from a JSON object.
Any suggestions would be greatly appreciated :)
You are getting that error because you are trying to deserialize the JSON into a List<T> for some T (specifically Term), but the root JSON container is not an array, it is an object -- an unordered set of key/value pairs surrounded by { and } -- that contains a fairly deeply embedded collection of objects corresponding to your Term.
Given that, you could use http://json2csharp.com/ or Paste JSON as Classes to auto-generate a complete data model corresponding to your JSON, then deserialize to that model and select out the interesting portions.
If, however, you don't want to define a complete data model, you can selectively deserialize only the relevant portions by loading your JSON into an intermediate JToken hierarchy and then using
SelectTokens():
var root = JToken.Parse(responseString);
var searchTermList = root.SelectTokens("myTerms[*].Term")
.Select(t => t.ToObject<Term>())
.ToList();
Notes:
The query string "myTerms[*].Term" contains the JSONPath wildcard operator [*]. This operator matches all array elements under the parent element "myTerms".
Json.NET supports JSONPath syntax as documented in Querying JSON with JSONPath.
If the JSON is more complex than is shown in your question, you can use the JSONPath recursive descent operator ... instead to find Term objects at any level in the JSON object hierarchy, e.g.:
var searchTermList = root.SelectTokens("..Term")
.Select(t => t.ToObject<Term>())
.ToList();
Once the relevant JSON objects have been selected you can use Jtoken.ToObject<Term>() to deserialize each one to your final c# model.
Sample fiddle.
Give this a try
public class Term
{
public string Known { get; set; }
public string Word { get; set; }
}
public class Response
{
public List<TermWrapper> MyTerms { get; set; }
}
public class TermWrapper
{
public Term Term { get; set; }
}
...
var response = await httpClient.GetAsync(uri);
string responseString = response.Content.ReadAsStringAsync().GetResults();
var searchTermList = JsonConvert
.DeserializeObject<Response>(responseString)
.MyTerms
.Select(x => x.Term);
You have to consider the full structure of the JSON. You have an object with 3 properties. The one you are interested in is an Array of Objects, but the objects are not terms, they are objects with a property called "Term". That property itself is of type Term. By creating the classes with similar structure you can then pull all the necessary data out of the structure.

How to map json and skip parent property?

I have this json string, which contains two elements each with a Number and a Status:
var jsonString = "{\"Errors\":[{\"Number\":9,\"Status\":\"BadRequest\"}, {\"Number\":3,\"Status\":\"BadConnection\"}]}";
As you see it has a parent property called Errors.
I have prepared this model:
public class ExceptionStructure
{
public int Number { get; set; }
public string Status { get; set; }
}
Using NewtonSoft.Json I would like to deserialize the json string into an array of ExceptionStructure objects, without also having to create a model for the parent property (as I don't really need it).
Can I do this (perhaps with some json attribute on the model class)?
I was hoping to do something like this to deserialize:
var exceptionArr = JsonConvert.DeserializeObject<ExceptionStructure>(jsonString);
JSON.NET allows you to deserialize parts of a json file. You can do this by first deserialzing the json string to a JObject, extract the relevant parts, and then deserialize those to your actual object.
JObject errors = JObject.Parse(jsonString);
IList<JToken> results = errors["Errors"].Children().ToList();
IList<ExceptionStructure> exceptions = new List<ExceptionStructure>();
foreach (JToken result in results)
{
ExceptionStructure exception= result.ToObject<ExceptionStructure>();
exceptions.Add(exception);
}
Honestly though, in your case it might be easier to just build a Errors parent class
More information can be found at http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm
this is may be helpful you.
string s = "{\"Errors\":[{\"Number\":9,\"Status\":\"BadRequest\"}, {\"Number\":3,\"Status\":\"BadConnection\"}]}";
var jobj = JObject.Parse(s);
List<ExceptionStructure> list = jobj["Errors"].ToObject<List<ExceptionStructure>>();
OR:
string s = "{\"Errors\":[{\"Number\":9,\"Status\":\"BadRequest\"}, {\"Number\":3,\"Status\":\"BadConnection\"}]}";
List<ExceptionStructure> list = JObject.Parse(s)
.SelectToken("Errors")
.ToObject<List<ExceptionStructure>>();

C# Parse/Deserialize JSON partially with Newtonsoft

I have to extract a part of json-string using .net or newtonsoft json.
JSON:
var json = "{\"method\":\"subtract\",\"parameters\":{\"minuend\":\"SOME_CUSTOM_JSON_OBJECT_DIFFERENT_FOR_EACH_METHOD\",\"subtrahend\":23}}";
C# Class:
class MyJson{
public string method { get; set; }
//public string parameters {get; set;}
public object parameters {get; set;}
}
I do not need to parse all the children of "parameters" json-object. "parameters" could be a very big object ([{obj1}...{obj1000}], objX of 1000 fields), parse which would be not performant.
I would like i.e. to pass it exactly as it is on some point, so conversion "string-C#object-string" would be redundant.
I do not want use Regexp or string transformations (string.Substring, Split and co), because of error margin, I know that all .net and newtonsoft string transformations based.
Question 1: if I define a property of type "object", how newtonsoft will handle this? (Documentation is worse than msdn, so I'm looking for the input from you, who already tried this).
static void Main(string[] args)
{
var json = "{\"method\":\"subtract\",\"parameters\":{\"minuend\":42,\"subtrahend\":23}}";
var data = JsonConvert.DeserializeObject<MyJson>(j);
// what internal representaion of data.parameters?
// How is it actually converted from json-string to an C# object (JObject/JsonObject).
}
In perfect case:
"parameters" is a string and calling
ExtractMyJson(jsonString)
gives me the json string of parameters.
Basically I need the newtonsoft version of
string ExtractMyJson(jsonString){
var p1 = jsonString.Split(",");
// .. varios string transformations
return pParams;
}
Note: please don't reference "dynamic" keyword or ask why no string transformations, it's the very specific question.
If you know that your parameters are unique you can do something like this:
class MyJson
{
public string method { get; set; }
public Dictionary<string,object> parameters { get; set; }
}
................
string json = "{\"method\":\"subtract\",\"parameters\":{\"minuend\":{\"img\": 3, \"real\": 4},\"subtrahend\":23}}";
var data = JsonConvert.DeserializeObject<MyJson>(json);
If you let it as object is going to receive the type Newtonsoft.Json.Linq.JObject.
Have you tried JTOKEN?
It is a rather simple solution to partially read basic or nested JSONs as described in this post.
For a nested JSON
{
"key1": {
"key11": "value11",
"key12": "value12"
}
"key2": "value2"
}
it would look like this
JToken token = JToken.Parse(json);
var value12 = token.SelectToken("key1.key12");
to get the element of the key "key12.
I think this could go nicely with your problem.
Well Objects are treated the same way your parent object is treated. It will start from the base of the graph. So if you have something like:
Person
{
Address Address {get;set;}
}
The Json will start Deserializing Address and then add in the Person object.
If you want to limit thesize of the graph depth you can use a setting like :
JsonConvert.DeserializeObject<List<IList<IList<string>>>>(json, new JsonSerializerSettings
{
MaxDepth = 2
});
For more configurations of the JsonSerializer check JsonSerializerSettings
If your field is an object then that object will have the KeyValuePair of every property that it holds, based on that when you cast that field you can access that type.(the behaviour is the same as assigning a type to an object in C#).
Update: So if you question using JsonObject or type, well JObject is and intermediary way to construct the json format in a generic format. But using the Type deserializatin means you can ignore properties you are not interested in. Mapping to a json with a type makes more sense because it creates a new object and dismisses the old JObject.

Deserialize JSON data

I've got a JSON data from Twitter API SEARCH.
I'm trying to deserialize these data into objects.
The JSON scheme looks like this:
{
"element": INT,
"element2": STRING,
..
..
"Results":[
{
"user":STRING,
"image":STRING,
..
..
}
]
}
How could I deserialize those JSON elements into objects using JSON Toolkit or something else?
Create a class that matches the JSON schema
public class Data
{
public string Element{get;set;}
public string Element2{get;set;}
public List<Result> Results{get;set;}
}
public class Result
{
public string User{get;set;}
public string Image{get;set;}
}
and use JSON.NET to deserialize
var result = JsonConvert.DeserializeObject<Result>(json);
If you have problems with correct type definition, you can always use dynamic deserialization using Json.Net:
var original = JsonConvert.DeserializeObject<dynamic>(jsonstring);
and then build your desired object based on it (if for example the original one contains overhead information set, and you don't need all of them):
var somepart = new {
E1 = original.element1,
E2 = original.element2
};

Json Stream in Wcf c# service to array

I'm really bad with C# and would like your help doing the following; I am currently passing a Json Array to my WCF webservice. I need to take the json array and insert it into a list. I have no idea to deserialize in C#. Please suggest to me what is the best way of achieving this. My code looks as follows:
public String UpdateOrderAddress(Stream userInfo)
{
try
{
StreamReader reader = new StreamReader(userInfo);
string JSONdata = reader.ReadToEnd();
if (JSONdata == null)
{
return "null";
}
return JSONdata; // Success !
}
catch (Exception e)
{
return e.ToString();
}
}
This is the data in the string that I get from the reader
[{"date":"2013-02-22 15:30:374:021","id":"1","description":"test","name":"test"},
"date":"2013-02-25 11:56:926:020","id":"2","description":"ghy","name":"fhh"},
"date":"2013-02-25 11:56:248:026","id":"3","description":"ghfm","name":"run"}]
The code you posted doesn't show how are you trying to deserialize your json string so I don't really follow what's the relevance here but in any case, this is how to deserialize JSON into a concrete C# class.
Create a class that matches the structure of your Javascript objects as so:
public class Data
{
public string Date {get;set;}
public int ID {get;set;}
public string Description {get;set;}
public string Name {get;set;}
}
Deserialize it using the JavascriptSerializer as so:
var deserializedData = new JavaScriptSerializer().Deserialize<List<Data>>(jsonString);
Note that your original JSON string is incorrectly formatted. It's missing the opening { on each element of the array. It should really be:
[{"date":"2013-02-22
15:30:374:021","id":"1","description":"test","name":"test"},
{"date":"2013-02-25
11:56:926:020","id":"2","description":"ghy","name":"fhh"},
{"date":"2013-02-25
11:56:248:026","id":"3","description":"ghfm","name":"run"}]
Now, if you attempt to deserialize the above, as so:
string json = #"[{""date"":""2013-02-22 15:30:374:021"",""id"":""1"",""description"":""test"",""name"":""test""},
{""date"":""2013-02-25 11:56:926:020"",""id"":""2"",""description"":""ghy"",""name"":""fhh""},
{""date"":""2013-02-25 11:56:248:026"",""id"":""3"",""description"":""ghfm"",""name"":""run""}]";
var deserializedData = new JavaScriptSerializer().Deserialize<List<Data>>(json);
You'll get a nice List<Data> back.
Also note that I didn't use a DateTime field for the corresponding date field in your Javascript object, the reason being that your sample dates are not valid DateTimes or at least a DateTime object cannot be created from that string representation. For instance, "15:30:374:021" makes no sense - I would imagine that 374 is the seconds field...
You need to add a reference to System.Web.Extensions to be able to use the JavascriptSerializer.
You can create a representative class with your required properties to hold the values and use the JavascriptSerializer class. Call the Deserialize<T> method specifying your type to deserialize the JSON into your code.
Links in class names for reference.
You can use Newtonsoft.Json Library. All you will need to do is:
List<YourClass> yourClassList = JsonConvert.DeserializeObject<List<YourClass>>(JSONdata);
You find more information, even samples here

Categories