2013-08-24 6 views
1

DataContractJsonSerializer를 사용하여 Flickr에서 일부 JSON 데이터를 구문 분석하려고합니다. JSON 응답을 받고 스트림에서 문제없이 계속 수행하고 있습니다. 파일에 쓰면 JSON이 모두 있습니다.DataContractJsonSerializer를 사용하여 JSON 응답을 구문 분석

jsonFlickrApi({"photos":{"page":1, "pages":372738, "perpage":10, "total":"3727375", "photo":[{"id":"9578613971", "owner":"[email protected]", "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

에서 아무 것도 얻을 code from msdn.microsoft.com/en-us/library/hh674188.aspx 이 단계를 완료하는 데 약간의 도움을 주시면 감사하겠습니다. 끝에서 "(JSON의 시작과에서") "jsonFlickrApi"를 제거

+0

케이스 관련 있습니까? json은 소문자 값을 가지고 있습니다. 클래스는 파스칼 케이스입니다. –

+0

요소가 배열이기 때문에 느낌이 들었습니다. 이제 어떻게 처리 할 수 ​​있는지 알아야합니다. –

답변

2

시도 ..

자바 스크립트를 사용하지 않기 때문에 내가, 당신이 여기에서 필요로하지 않는 API에 대한 콜백 PARAM을 보낼 생각 응답을 구문 분석하려면

+0

ok 문자열로 변환 한 후 JSON 응답의 시작과 끝을 다듬었습니다. 그런 다음 MemoryStream으로 다시 변환하고 필요한 경우 0으로 이동하여 스트림으로 구문 분석 할 수 있습니다 DataContractJsonSerializer 및 ... 여전히 아무것도. 디버거는 시리얼 라이저를 통과 할 때까지 데이터가 있음을 보여줍니다. –

+0

RootObject를 사용하지 않고 Photos 요소를 사용하는 것은 실수였습니다. 마지막으로 jsonReponse 객체에서 데이터를 반환합니다! jsoncallback의 제거에 관해서 그 머리에 감사드립니다. –

+1

아! 당신은 방금 저를 구해주었습니다 !!! 문제를 파악하는 데 많은 시간을 할애했습니다. +1은 가장 쉽지만 까다로운 해결책입니다. :디 –

관련 문제