how to work with json object in c#

2020-06-14 06:07发布

问题:

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
  }
}

回答1:

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



回答2:

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;
    }


回答3:

The easiest way to do this is to create a class that matches your JSON, deserialize the JSON and then access the properties through the created object.

For example:

public class Companies(){
  public double Popularity { get; set; }
  public List<ProductionCompany> Production_Companies { get;set; }

  public Companies(){
    Production_Companies  = new List<ProductionCompany>();
  }
}

public class ProductionCompany(){
   public string Name {get;set;}
   public int Id {get;set;}
}

Then you deserialize with JSON.Net

  var myObject = JsonConvert.DeserializeObject<Companies>(jsonString);

And access the Properties

foreach (var company in myObject.Production_Companies)
{
   company.Name;  //do something...
}