【问题标题】:Parsing Flickr JSON Responses with DataContractJsonSerializer使用 DataContractJsonSerializer 解析 Flickr JSON 响应
【发布时间】:2013-08-24 09:11:25
【问题描述】:

所以我尝试使用 DataContractJsonSerializer 从 Flickr 解析一些 JSON 数据 我正在接收 JSON 响应并将其保存在 Stream 中没有问题,这意味着我已经通过写入文件的方式对其进行了测试,并且 JSON 就在那里。

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; }
    }

我的代码如下所示:

            // 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();

但我在 jsonResponse 中一无所获

从 msdn.microsoft.com/en-us/library/hh674188.aspx 获得了一些代码 如果能帮我完成这一步,我们将不胜感激。

【问题讨论】:

  • 与大小写有关吗? json 有小写值,类是 pascal 大小写...
  • 我有一种感觉,因为 元素是一个数组。现在我要看看我该如何处理。

标签: c# json flickr


【解决方案1】:

尝试从 JSON 开头删除“jsonFlickrApi(”,从结尾删除“)”..

我认为您正在向此处不需要的 API 发送回调参数,因为您没有使用 JavaScript 来解析响应

【讨论】:

  • 好的,在将 JSON 响应转换为字符串后,我已经修剪了 JSON 响应的开头和结尾...然后我将其转换回 MemoryStream,将搜索定位到 0 以防万一它需要,这样我就可以现在通过 DataContractJsonSerializer 将其解析为流,然后......仍然没有。调试器显示数据在通过序列化程序之前一直存在。
  • 我没有使用 RootObject,使用 Photos 元素是一个错误。最后,我的数据在 jsonReponse 对象中返回!感谢您对删除 jsoncallback 的提醒。
  • 啊啊啊!!!你刚刚救了我男人!!!花了很多时间来解决问题。 +1 是最简单但棘手的解决方案。 :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 2018-09-14
相关资源
最近更新 更多