我在"data"中有一个带有多个电影标题的json响应。有没有办法快速提取它们?我需要一个仅包含电影标题的数组。

{
  "page": "2",
  "per_page": 10,
  "total": 13,
  "total_pages": 2,
  "data": [{
    "Poster": "N/A",
    "Title": "They Call Me Spiderman",
    "Type": "movie",
    "Year": 2016,
    "imdbID": "tt5861236"
  }, {
    "Poster": "N/A",
    "Title": "The Death of Spiderman",
    "Type": "movie",
    "Year": 2015,
    "imdbID": "tt5921428"
  }, {
    "Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BZDlmMGQwYmItNTNmOS00OTNkLTkxNTYtNDM3ZWVlMWUyZDIzXkEyXkFqcGdeQXVyMTA5Mzk5Mw@@._V1_SX300.jpg",
    "Title": "Spiderman in Cannes",
    "Type": "movie",
    "Year": 2016,
    "imdbID": "tt5978586"
  }]
}

最佳答案

您可以使用:


newtonsoft
C#System.dynamic
C#ExpandoObject Class


通过这种方式:

dynamic content = JsonConvert.DeserializeObject<ExpandoObject>(data);


像这样:

using System;
using System.Dynamic;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        string data = @"{
  'page': '2',
  'per_page': 10,
  'total': 13,
  'total_pages': 2,
  'data': [{
    'Poster': 'N/A',
    'Title': 'They Call Me Spiderman',
    'Type': 'movie',
    'Year': 2016,
    'imdbID': 'tt5861236'
  }, {
    'Poster': 'N/A',
    'Title': 'The Death of Spiderman',
    'Type': 'movie',
    'Year': 2015,
    'imdbID': 'tt5921428'
  }, {
    'Poster': 'https://images-na.ssl-images-amazon.com/images/M/MV5BZDlmMGQwYmItNTNmOS00OTNkLTkxNTYtNDM3ZWVlMWUyZDIzXkEyXkFqcGdeQXVyMTA5Mzk5Mw@@._V1_SX300.jpg',
    'Title': 'Spiderman in Cannes',
    'Type': 'movie',
    'Year': 2016,
    'imdbID': 'tt5978586'
  }]
}";
        dynamic content = JsonConvert.DeserializeObject<ExpandoObject>(data);
        int i;
        int len = content.data.Count;
        string result = "";
        string[] myArray;
        for (i = 0; i < len; i++)
        {
            result += content.data[i].Title; // Extract the movie title.
            result += ","; // Conact with commas.
        }

        result = result.Substring(0, result.Length - 1);
        myArray = result.Split(','); // Array of string with the movie titles.
        Console.WriteLine(myArray[0]);
    }
}


实际操作中:.NET Fiddle

关于c# - 从JSON中的数据中提取所有值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47319757/

10-13 06:21