1

我想问是否有人知道如何提取“名称”和“查询”并将其存储在数组列表中。

源文件:https ://api.twitter.com/1/trends/daily.json

4

2 回答 2

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;
} 
于 2012-06-06T15:48:28.643 回答
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;
    }
}
于 2012-06-06T15:44:10.870 回答