0

我在一个字符串中得到了这个 JSON 代码,我正试图从中获取原始标题,...... 我使用的是 JSON.NET,但它不起作用,因为每个 qoutation 都被一个“/”转义。如果我想在 C# 中将 /" 替换为 ",则什么也不会发生..

string s = "{\"page\":1,\"results\":[{\"adult\":false,\"backdrop_path\":\"/      mMblxwsvvJ9wu8CVXVVjX4MBuN2.jpg\",\"id\":957,\"original_title\":\"Spaceballs\",\"release_date    \":\"1987-06-24\",\"poster_path\":\"/9lBOjli8TrJghuzAulVNJvTNJCS.jpg\",    \"popularity\":3.25235847573851,\"title\":\"Spaceballs\",\"vote_average\":7.7,\"vote_count    \":35},{\"adult\":false,\"backdrop_path\":null,\"id\":56297,\"original_title\":\"Spaceballs:     The Documentary\",\"release_date\":\"2005-05-05\",\"poster_path\":\"/    kPVdkoT49zP8RcruDP3AtquZ7WT.jpg\",\"popularity\":0.46,\"title\":\"Spaceballs: The Documentary    \",\"vote_average\":6.0,\"vote_count\":1}],\"total_pages\":1,\"total_results\":2}"
4

2 回答 2

0

如果您知道模型类的详细信息,可以使用以下代码

ModelClass myDeserializedObj = (ModelClass)JavaScriptConvert.DeserializeObject(s, typeof(ModelClass));

这样您就不需要手动处理 JSON 读取部分。如果您使用的是 ASP.Net MVC,则可以进行自动模型绑定,以便 MVC 开箱即用地为您执行此操作。

于 2013-05-26T09:55:34.240 回答
0
string input ="{\"page\":1,\"results\":[{\"adult\":false,\"backdrop_path\":\"/  mMblxwsvvJ9wu8CVXVVjX4MBuN2.jpg\",\"id\":957,\"original_title\":\"Spaceballs\",\"release_date\":\"1987-06-24\",\"poster_path\":\"/9lBOjli8TrJghuzAulVNJvTNJCS.jpg\",\"popularity\":3.25235847573851,\"title\":\"Spaceballs\",\"vote_average\":7.7,\"vote_count\":35},{\"adult\":false,\"backdrop_path\":null,\"id\":56297,\"original_title\":\"Spaceballs: The Documentary\",\"release_date\":\"2005-05-05\",\"poster_path\":\"/kPVdkoT49zP8RcruDP3AtquZ7WT.jpg\",\"popularity\":0.46,\"title\":\"Spaceballs: The Documentary\",\"vote_average\":6.0,\"vote_count\":1}],\"total_pages\":1,\"total_results\":2}";
dynamic d = JObject.Parse(input);
foreach (var item in d.results)
{
    Console.WriteLine(item.original_title);
}

如果您有模型类,您可以执行以下操作

var op = JsonConvert.DeserializeObject<AllResults>(input);
foreach (var item in op.results)
{
    Console.WriteLine(item.original_title);
}

public class Result
{
    public bool adult { get; set; }
    public string backdrop_path { get; set; }
    public int id { get; set; }
    public string original_title { get; set; }
    public string __invalid_name__release_date { get; set; }
    public string poster_path { get; set; }
    public double popularity { get; set; }
    public string title { get; set; }
    public double vote_average { get; set; }
    public int __invalid_name__vote_count { get; set; }
    public string release_date { get; set; }
    public int? vote_count { get; set; }
}

public class AllResults
{
    public int page { get; set; }
    public List<Result> results { get; set; }
    public int total_pages { get; set; }
    public int total_results { get; set; }
}

http://json2csharp.com/生成的类

于 2013-05-26T10:16:37.130 回答