我想问一下是否有人知道如何提取“名称”和“查询”,并可能将其存储在arraylist中。
答案 0 :(得分:2)
您可以使用JObject,例如: -
string response = requestData("https://api.twitter.com/1/trends/daily.json");
JObject jsonResponse = new JObject();
var name = string.Empty;
var query = string.Empty;
try
{
jsonResponse = JObject.Parse(response);
name = (string)jsonResponse["name"];
query = (string)jsonRespone["query"];
}
catch
{
return "";
}
public string requestData(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string results = sr.ReadToEnd();
sr.Close();
return results;
}
答案 1 :(得分:1)
基于这个问题:Parse JSON in C#
创建一个代表您正在提取的JSON的类,并使用链接问题中JSONHelper
类中的代码从JSON中提取类:
public class JSONHelper
{
public static T Deserialise<T>(string json)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer serialiser = new DataContractJsonSerializer(obj.GetType());
ms.Close();
return obj;
}
}