我正在尝试将包含Json(pinterest)的字符串转换为c#对象。我正在使用Json .NET,无论我尝试什么,data.pins.images内部文本总是转换为null。它显然不是空的。
c#类是由在线转换器创建的。实际的Json''pins'数组包含50个元素。为了简化问题,我除去了其中一个以外的所有内容。
下面列出了Json文本和测试程序。请帮忙。
using System.IO;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
//using System.Runtime.Serialization.Json;
namespace PinterestConsoleTest
{
class Program
{
static void Main(string[] args)
{
string jsonString =
"{"+
"\"status\": \"success\","+
"\"code\": 0,"+
"\"host\": \"ngapi2-67c11435\","+
"\"generated_at\": \"Sun, 07 Dec 2014 12:15:10 +0000\","+
"\"message\": \"ok\","+
"\"data\": "+
"{"+
"\"pins\": "+
"["+
"{"+
"\"attribution\": null,"+
"\"description\": \"Roasted Potato Salad with Pancetta, Sun-Dried Tomatoes and Arugula\","+
"\"pinner\": "+
"{"+
"\"about\": \"\","+
"\"location\": \"\","+
"\"full_name\": \"Anselma Berumen\","+
"\"follower_count\": 15,"+
"\"image_small_url\": \"http://passets-ak.pinterest.com/images/user/default_30.png\","+
"\"pin_count\": 1683,"+
"\"id\": \"460422899312895060\","+
"\"profile_url\": \"http://www.pinterest.com/chemita1331/\""+
"},"+
"\"repin_count\": 0,"+
"\"dominant_color\": \"#b7947a\","+
"\"like_count\": 0,"+
"\"link\": \"http://www.domesticate-me.com/roasted-potato-salad-with-pancetta-sun-dried-tomatoes-and-arugula/\","+
"\"images\": "+
"{"+
"\"image\": "+
"{"+
"\"url\": \"http://media-cache-ec0.pinimg.com/237x/a0/97/9e/a0979eb898eee640eb980181035e75bc.jpg\","+
"\"width\": 237,"+
"\"height\": 334"+
"}" +
"},"+
"\"embed\": null,"+
"\"is_video\": false,"+
"\"id\": \"460422761878479851\""+
"}"+
"],"+
"\"user\": "+
"{"+
"\"about\": \"\","+
"\"location\": \"\","+
"\"full_name\": \"Anselma Berumen\","+
"\"follower_count\": 15,"+
"\"image_small_url\": \"http://passets-ak.pinterest.com/images/user/default_30.png\","+
"\"pin_count\": 1683,"+
"\"id\": \"460422899312895060\","+
"\"profile_url\": \"http://www.pinterest.com/chemita1331/\""+
"},"+
"\"board\": "+
"{"+
"\"description\": \"\","+
"\"url\": \"/chemita1331/salads/\","+
"\"follower_count\": 4,"+
"\"image_thumbnail_url\": \"http://media-cache-ak0.pinimg.com/upload/460422830593447062_board_thumbnail_2014-09-18-00-49-37_15359_60.jpg\","+
"\"pin_count\": 237,"+
"\"id\": \"460422830593447062\","+
"\"name\": \"Salads\""+
"}"+
"}"+
"}";
JsonPinterestTester jsonPinterestTester = new JsonPinterestTester();
PinsOfBoardRequest pinsOfBoardRequest = jsonPinterestTester.Parse(jsonString);
PinsOfBoardRequestImages images = pinsOfBoardRequest.data.pins[0].images;
Image237x image = pinsOfBoardRequest.data.pins[0].images.image237x;
Console.ReadKey();
}
}
class JsonPinterestTester
{
public PinsOfBoardRequest Parse(string jsonSource)
{
PinsOfBoardRequest request =
Newtonsoft.Json.JsonConvert.DeserializeObject<PinsOfBoardRequest>(jsonSource);
return request;
}
}
public class Image237x
{
public string url { get; set; }
public int width { get; set; }
public int height { get; set; }
}
public class PinsOfBoardRequestImages
{
public Image237x image237x { get; set; }
}
public class PinsOfBoardRequestPinner
{
public string about { get; set; }
public string location { get; set; }
public string full_name { get; set; }
public int follower_count { get; set; }
public string image_small_url { get; set; }
public int pin_count { get; set; }
public string id { get; set; }
public string profile_url { get; set; }
}
public class PinsOfBoardRequestUser
{
public string about { get; set; }
public string location { get; set; }
public string full_name { get; set; }
public int follower_count { get; set; }
public string image_small_url { get; set; }
public int pin_count { get; set; }
public string id { get; set; }
public string profile_url { get; set; }
}
public class PinsOfBoardRequestBoard
{
public string description { get; set; }
public string url { get; set; }
public int follower_count { get; set; }
public string image_thumbnail_url { get; set; }
public int pin_count { get; set; }
public string id { get; set; }
public string name { get; set; }
}
public class PinsOfBoardRequestPin
{
public object attribution { get; set; }
public string description { get; set; }
public PinsOfBoardRequestPinner pinner { get; set; }
public int repin_count { get; set; }
public string dominant_color { get; set; }
public int like_count { get; set; }
public string link { get; set; }
public PinsOfBoardRequestImages images { get; set; }
public object embed { get; set; }
public bool is_video { get; set; }
public string id { get; set; }
}
public class PinsOfBoardRequestData
{
//public IList<PinsOfBoardRequestPin> pins { get; set; }
public PinsOfBoardRequestPin[] pins { get; set; }
public PinsOfBoardRequestUser user { get; set; }
public PinsOfBoardRequestBoard board { get; set; }
}
public class PinsOfBoardRequest
{
public string status { get; set; }
public int code { get; set; }
public string host { get; set; }
public string generated_at { get; set; }
public string message { get; set; }
public PinsOfBoardRequestData data { get; set; }
}
}
答案 0 :(得分:1)
当然,Image237x
类的PinsOfBoardRequestImages
属性应命名为image
public class PinsOfBoardRequestImages
{
public Image237x image { get; set; }
}
另外,如果名称images
可能会推断出一个数组,请仔细检查您的样本是否与实际输入匹配
真的是:
images: { image: { ..
或者是:
images: [{ image: {.. , image { ...] ?
在这种情况下,DTO将是一个数组:
public Image237x[] image { get; set; }