我在字符串中有这个JSON代码,我正试图获得原始标题,......出来。我使用的是JSON.NET,但它无法工作,因为每个qoutation都被'/'转义。如果我想在C#中替换/“to”,则没有任何反应..
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}"
答案 0 :(得分:0)
如果您知道模型类详细信息,则可以使用以下代码
ModelClass myDeserializedObj = (ModelClass)JavaScriptConvert.DeserializeObject(s, typeof(ModelClass));
这样您就不需要手动处理JSON读取部分。如果您使用的是ASP.Net MVC,则可以进行自动模型绑定,以便MVC为您提供开箱即用的功能。
答案 1 :(得分: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; }
}
生成的类