1

JSON 文字列を逆シリアル化しようとしていますがEnd element 'caption' from namespace '' expected. Found element 'created_time' from namespace ''.、呼び出し時にエラーが発生します

InstagramObject test = (InstagramObject) instagramObject.ReadObject(instagramWebResponse.GetResponseStream());

Instagram の json は多くの要素を返しますが、必要なものはごくわずかです。私の DataContract は以下に掲載されます。

基本的に私は必要です{data:[{caption: string, link: string, images: {...}}]}

これがjsonの例です...

{
"data": [{
    "comments": {
        "data": [],
        "count": 0
    },
    "caption": {
        "created_time": "1296710352",
        "text": "Inside le truc #foodtruck",
        "from": {
            "username": "kevin",
            "full_name": "Kevin Systrom",
            "type": "user",
            "id": "3"
        },
        "id": "26621408"
    },
    "likes": {
        "count": 15,
        "data": [{
            "username": "mikeyk",
            "full_name": "Mike Krieger",
            "id": "4",
            "profile_picture": "..."
        }, {...subset of likers...}]
    },
    "link": "http://instagr.am/p/BWrVZ/",
    "user": {
        "username": "kevin",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
        "id": "3"
    },
    "created_time": "1296710327",
    "images": {
        "low_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_6.jpg",
            "width": 306,
            "height": 306
        },
        "thumbnail": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_5.jpg",
            "width": 150,
            "height": 150
        },
        "standard_resolution": {
            "url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_7.jpg",
            "width": 612,
            "height": 612
        }
    },
    "type": "image",
    "filter": "Earlybird",
    "tags": ["foodtruck"],
    "id": "22721881",
    "location": {
        "latitude": 37.778720183610183,
        "longitude": -122.3962783813477,
        "id": "520640",
        "street_address": "",
        "name": "Le Truc"
    }
}]
} 

ここに私のDataContractがあります...

[DataContract]
public class InstagramObject {

    [DataMember(Name = "data")]
    public IEnumerable<InstagramData> instagramData { get; set; }

}

[DataContract]
public class InstagramData {

    [DataMember(Name = "images")]
    public List<InstagramImage> instagramImages { get; set; }

    [DataMember(Name = "caption")]
    public string caption { get; set; }

    [DataMember(Name = "link")]
    public string link { get; set; }

}

[DataContract]
public class InstagramImage {

    [DataMember(Name = "low_resolution")]
    public InstagramImageInfo lowResolutionImage { get; set; }

    [DataMember(Name = "thumbnail")]
    public InstagramImageInfo thumbnail { get; set; }

    [DataMember(Name = "standard_resolution")]
    public InstagramImageInfo standardResolution { get; set; }

}

[DataContract]
public class InstagramImageInfo {

    [DataMember(Name = "url")]
    public string url { get; set; }

    [DataMember(Name = "width")]
    public int width { get; set; }

    [DataMember(Name = "height")]
    public int height { get; set; }

}
4

2 に答える 2

1

データ コントラクトcaptionでは文字列として宣言されますが、JSONcaptionではさまざまなフィールドを持つレコードですcaption。互換性のある構造を持つオブジェクトとして宣言する必要があります。

[DataContract]  
public class InstagramCaption {

   [DataMember(Name = "text")]
   public string Text {get; set;}

} 

. . . 

  [DataMember(Name = "caption")]     
  public InstagramCaption caption { get; set; }     

. . . 
于 2012-08-21T13:42:23.050 に答える
-1

これらのクラスを宣言する必要はありませんJson.Net + Linqを使用する場合

var obj = (JObject)JsonConvert.DeserializeObject(json);
var result = obj["data"]
    .Select(x => new {
        Caption = (string)x["caption"]["text"],
        Link = (string)x["link"],
        Images = x["images"].Children()
                            .Select(y=>(string)y.First()["url"]).ToArray()
    })
    .ToArray();
于 2012-08-21T14:06:36.907 に答える