我已经得到了这个JSON代码的字符串,我试图得到原来的标题,…离开它。我使用JSON。. NET,但它不会工作,因为每个引号都由'/'转义。如果我想替换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}"
如果您知道模型类的详细信息,您可以使用以下代码
ModelClass myDeserializedObj = (ModelClass)JavaScriptConvert.DeserializeObject(s, typeof(ModelClass));
这样就不需要手动处理JSON读取部分。如果你正在使用ASP。Net MVC,你可以做自动模型绑定让MVC为你开箱操作
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/生成的类