Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to work with json object in c#

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
  }
}
like image 375
Hooman Avatar asked Aug 23 '16 10:08

Hooman


People also ask

What is JSON object in C?

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.

Can C use JSON?

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.

What is [] and {} in JSON?

' { } ' used for Object and ' [] ' is used for Array in json.

What are JSON objects with example?

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.


2 Answers

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

like image 105
Grappachu Avatar answered Oct 16 '22 12:10

Grappachu


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;
    }
like image 24
Maverick Avatar answered Oct 16 '22 11:10

Maverick