解析JSON + API [英] Parsing JSON + API
本文介绍了解析JSON + API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个返回JSON文本的API.我需要在结果中的某些特定字段中搜索文本.我有几个问题:
I am using an api which returns JSON text. I need to search text in some specific fields in the results. I have a few problems:
- 每次我使用具有不同术语的API时,都会得到不同的字段,所以我无法编写结果的类,因为每次结果字段都不同.有没有 是一种无需创建类即可解析JSON的方法吗?也许它可以通过字段名称自动创建?
- Each time I am using the API with diffrent terms I get different fields, so I can't write a Class for the results because each time the results fields are different. Does there is a way to parse the JSON without creating a class for it?, Maybe it can create autmatically by the field name?
- I Dont know how to call for the API.
希望您能帮我解决这些问题.
I hope that you can help me with the questions.
谢谢
推荐答案
您可以使用JSON.Net库:
You can use JSON.Net library to do so:
var o = JObject.Parse(stringFullOfJson);
var page = (int)o["page"];
var totalPages = (int)o["total_pages"];
参考: http://stackoverflow.com/a/4749755/1184708
或者您可以使用Newtonsoft的库:
Or you can use Newtonsoft's library:
dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
var page = x.page;
var total_pages = x.total_pages
var albums = x.albums;
foreach(var album in albums)
{
var albumName = album.name;
//access album data;
}
参考: http://stackoverflow.com/a/25542532/1184708
这篇关于解析JSON + API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文