I'm working with a json which comes from an API, here is what I'm talking about:
{
"popularity": 3.518962,
"production_companies": [
{
"name": "value1",
"id": 4
},
{
"name": "value2",
"id": 562
},
{
"name": "value13",
"id": 14654
},
{
"name": "value4",
"id": 19177
},
{
"name": "value5",
"id": 23243
}
]
}
I already can return value of popularity
As an example I need to know how can I access value of name
and which name
it is?
I also tried to convert it to an array but didn't work or I did something wrong.
Movie class :
public class Movie {
public string popularity {get; set;}
public object production_companies {get; set;}
public Movie GetBasic(string id) {
string json = @"{
"popularity": 3.518962,
"production_companies": [
{
"name": "value1",
"id": 4
},
{
"name": "value2",
"id": 562
},
{
"name": "value13",
"id": 14654
},
{
"name": "value4",
"id": 19177
},
{
"name": "value5",
"id": 23243
}
]
}";
Movie Data = JsonConvert.DeserializeObject<Movie>(json);
return Data;
}
What I've done so far:
@{
var arr = Item.production_companies.ToString().Substring(1, (Item.production_companies.ToString().Length - 2)).ToArray();
foreach(var a in arr) {
@a.name
}
}
JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. JSON stands for JavaScript Object Notation. The format was specified by Douglas Crockford.
JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects. It aims to conform to RFC 7159.
' { } ' used for Object and ' [] ' is used for Array in json.
JSON Object Example A JSON object contains data in the form of key/value pair. The keys are strings and the values are the JSON types. Keys and values are separated by colon. Each entry (key/value pair) is separated by comma.
After you get a json string you need to deserialize it. Use this site to generate you model
http://json2csharp.com/
you will get some classes like
public class ProductionCompany
{
public string name { get; set; }
public int id { get; set; }
}
public class RootObject
{
public double popularity { get; set; }
public List<ProductionCompany> production_companies { get; set; }
}
then you can call
var json = "...yout json string..."
RootObject obj = JsonConvert.DeserializeObject<RootObject >(json);
and you can use the data retreived easily
First Define the Classes as below :
public class ProductionCompany
{
public string name { get; set; }
public int id { get; set; }
}
public class Item
{
public double popularity { get; set; }
public List<ProductionCompany> production_companies { get; set; }
}
You can then use jsonSerializer to convert your JSON to class object
string jsonInput="your Json string";
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
Item item= jsonSerializer.Deserialize<Item>(jsonInput)
Now your data can easily be retrived from object item, something like this
foreach (var productioncompany in item.Production_Companies)
{
productioncompany.Name;
productioncompany.id;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With