C# 使用DataContractJsonSerializer解析Flickr JSON响应
所以我尝试使用DataContractJsonSerializer解析Flickr中的一些JSON数据 我正在接收JSON响应并将其保存在一个流中,没有问题,这意味着我已经通过将其写入文件进行了测试,JSON就在那里C# 使用DataContractJsonSerializer解析Flickr JSON响应,c#,json,flickr,C#,Json,Flickr,所以我尝试使用DataContractJsonSerializer解析Flickr中的一些JSON数据 我正在接收JSON响应并将其保存在一个流中,没有问题,这意味着我已经通过将其写入文件进行了测试,JSON就在那里 jsonFlickrApi({"photos":{"page":1, "pages":372738, "perpage":10, "total":"3727375", "photo":[{"id":"9578613971", "owner":"7960563@N07", "secr
jsonFlickrApi({"photos":{"page":1, "pages":372738, "perpage":10, "total":"3727375", "photo":[{"id":"9578613971", "owner":"7960563@N07", "secret":"b7b80b75f8", "server":"3734", "farm":4, "title":"1970 - 1978 Toyota Corolla E20 Coup\u00e9", "ispublic":1, "isfriend":0, "isfamily":0, "url_t":"http:\/\/farm4.staticflickr.com\/3734\/9578613971_b7b80b75f8_t.jpg", "height_t":"67", "width_t":"100", "url_o":"http:\/\/farm4.staticflickr.com\/3734\/9578613971_0eda23bccb_o.jpg", "height_o":"1000", "width_o":"1500"}}]}, "stat":"ok"})
但是当我尝试使用Jsonserializer进行解析时,我注意到它不包含任何内容
感谢Json2Cshap.com,我已经建立了合同类
public class ResponseContract
{
[DataContract]
public class Photo
{
[DataMember]
public string id { get; set; }
[DataMember]
public string owner { get; set; }
[DataMember]
public string secret { get; set; }
[DataMember]
public string server { get; set; }
[DataMember]
public int farm { get; set; }
[DataMember]
public string title { get; set; }
[DataMember]
public string url_t { get; set; }
[DataMember]
public string url_o { get; set; }
}
[DataContract]
public class Photos
{
[DataMember]
public int page { get; set; }
[DataMember]
public int pages { get; set; }
[DataMember]
public int perpage { get; set; }
[DataMember]
public string total { get; set; }
[DataMember]
public List<Photo> photoList { get; set; }
}
[DataContract]
public class RootObject
{
[DataMember]
public Photos photos { get; set; }
[DataMember]
public string stat { get; set; }
}
但我没有得到任何回应
从msdn.microsoft.com/en-us/library/hh674188.aspx获得一些代码
如果您能帮我完成这一步,我将不胜感激。请尝试从末尾删除“jsonFlickrApi(“从JSON开始和”)。”
我认为您正在向API发送一个回调参数,这里不需要它,因为您没有使用JavaScript解析响应它与大小写相关吗?json有小写的值,类是pascal大小写的…我感觉到它是一个数组,因为元素是一个数组。现在我来看看如何处理这个问题。好吧,在将JSON响应转换为字符串后,我修剪了JSON响应的开头和结尾…然后我将其转换回MemoryStream,将seek定位为0(如果需要),这样我现在就可以通过DataContractJsonSerializer将其解析为流,但仍然什么都没有。调试器显示数据在通过序列化程序之前一直存在。我没有使用RootObject,使用Photos元素是一个错误。最后,我让我的数据在JSONResponse对象中返回!谢谢你提醒我移除jsoncallback。啊哈!!!你刚刚救了我,伙计!!!花了很多时间来解决这个问题+1是最简单但棘手的解决方案:D
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.longUrl);
// Send the request and wait for response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the response stream
Stream responseStream = response.GetResponseStream();
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Photos));
object objResponse = (Photos)jsonSerializer.ReadObject(responseStream);
Photos jsonResponse = objResponse as Photos;
response.Close();