如何将JSON转换为C#类?

时间:2013-04-25 18:33:05

标签: c# json json.net

请帮我把这个json转换成C#类

[{"size":[{"width":8,"height":8}]},
{"ppi":300,"theme":1},
{"cover":[{"type":"Hard Cover","title":"Photobook Title (Albumname)","image_url":"imagexyz.jpg"}]},
{"pages":[{"page":[{"pageid":"page0","layout":"one-tmp full",                                    "layout_type":"one-tmp full","background_image":"img/photobook/bg/12.jpg",
   "photos":[{"id":"pg0-photo1","image_url":"https://images/stephy/w692-692h/b6964ed0-ea70-4176-ba0e-a32d607baec9.jpg",
"crop":[{"width":617,"height":469,"top":0,"left":-1,"p_width":469,"p_height":469}]}]},              {"pageid":"page1","layout":"one-tmp full", "layout_type":"one-tmp full","background_image":"img/photobook/bg/12.jpg",
"photos":[{"id":"pg1-photo1","image_url":"https://images/stephy/w876-547h/e0c369d2-5617-46d3-970c-f9998bef23c3.jpg",
"crop":[{"width":617,"height":469,"top":0,"left": -1,"p_width":469,"p_height":469}]}]}
 ]         }] }]

我试图将此转换为this example,但它不起作用。它给了我一个JSON与该类对象不匹配的错误。请帮我转换它,如何更改“封面”:[“image_url”]?

1 个答案:

答案 0 :(得分:1)

你json是数组,所以在使用你提到的网站创建对象后,试试这个

var obj = JsonConvert.DeserializeObject<List<RootObject>>(json);

它会起作用......