这里的第一篇文章。而且,我会认为自己是一个非常非常低的入门级c#/asp.net/MSSQL开发人员,所以我的知识库就像冬天的松鼠坚果库一样大。
问题:无法从goo.gl分析的JSON响应中提取多层次(可能不是正确的术语)参数值。
我最近偶然发现了谷歌提供的goo.gl网址短程序接口,我很喜欢它!下面是我在http://www.jphellemons.nl/post/Google-URL-shortener-API-(googl)-C-sharp-class-C.aspx找到的短程序代码。
public static string Shorten(string url)
{
string key = "my_google_provided_API_key";
string post = "{\"longUrl\": \"" + url + "\"}";
string shortUrl = url;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + key);
try
{
request.ServicePoint.Expect100Continue = false;
request.Method = "POST";
request.ContentLength = post.Length;
request.ContentType = "application/json";
request.Headers.Add("Cache-Control", "no-cache");
using (Stream requestStream = request.GetRequestStream())
{
byte[] postBuffer = Encoding.ASCII.GetBytes(post);
requestStream.Write(postBuffer, 0, postBuffer.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader responseReader = new StreamReader(responseStream))
{
string json = responseReader.ReadToEnd();
shortUrl = Regex.Match(json, @"""id"": ?""(?<id>.+)""").Groups["id"].Value;
}
}
}
}
catch (Exception ex)
{
// if Google's URL Shortner is down...
System.Diagnostics.Debug.WriteLine(ex.Message);
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
return shortUrl;
}现在,goo.gl还提供了诸如创建时间、状态和短URL点击次数等分析。返回的JSON字符串采用以下格式:
{
"kind": "urlshortener#url",
"id": value,
"longUrl": value,
"status": value,
"created": value,
"analytics": {
"allTime": {
"shortUrlClicks": value,
"longUrlClicks": value,
"referrers":
[
{
"count": value,
"id": value
}, ...
],
"countries": [ ... ],
"browsers": [ ... ],
"platforms": [ ... ]
},
"month": { ... },
"week": { ... },
"day": { ... },
"twoHours": { ... }
}
}现在,我可以从JSON响应中提取第一个级别的参数值(id、status、longurl等),没有问题。当我想从“分析”中提取"shortUrlClicks“和"allTime”时,问题就出现了。我已经盯着谷歌的世界好几天了,至今还没有结果。而且,已经尝试过各种序列化程序,但仍然什么也没有。我一直用于提取id、status和longurl的内容是:
protected void load_analytics(string shorturl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?&shortUrl=" + shorturl+ "&projection=FULL");
request.ServicePoint.Expect100Continue = false;
request.Method = WebRequestMethods.Http.Get;
request.Accept = "application/json";
request.ContentType = "application/json; charset=utf-8";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader responseReader = new StreamReader(responseStream))
{
String json = responseReader.ReadToEnd();
String asdf = Regex.Match(json, @"""status"": ?""(?<status>.+)""").Groups["status"].Value;
}
}
}
}解决方案找到*解决方案找到*解决方案找到
谢谢你的答复。你不使用正则表达式是绝对正确的。在咨询了我的“大师”之后,我们发现了http://blogs.msdn.com/b/rakkimk/archive/2009/01/30/asp-net-json-serialization-and-deserialization.aspx。
最终的解决办法是:
-create用于表示JSON层次结构的类
public class Analytics
{
public Alltime alltime = new Alltime();
}
public class Alltime
{
public int ShortUrlClicks;
}响应流中的-next,反序列化为层次结构中的最高元素(Analytics类),并查看!
using (StreamReader responseReader = new StreamReader(responseStream))
{
String json = responseReader.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
//String asdf = Regex.Match(json, @"""status"": ?""(?<status>.+)""").Groups["status"].Value;
Analytics p2 = js.Deserialize<Analytics>(json);
String fdas = p2.alltime.ShortUrlClicks.ToString();
//note how i traverse through the classes where p2 is Analytics
//to alltime to ShortUrlClicks
} //fdas yields "0" which is correct since the shorturl I tested has never been clicked发布于 2011-07-13 19:21:38
查看Json.NET库,不要使用regex进行解析。
https://stackoverflow.com/questions/6684362
复制相似问题