json 데이터를 C#의 사용자 정의 모델로 변환 할 때 stucked가 발생했습니다. 복잡한 json 객체를 사용자 정의 데이터 모델로 비 직렬화 C#
{
"id": "100002231955291",
"albums": {
"data": [
{
"id": "103570756393989",
"name": "xyz",
"photos": {
"data": [
{
"id": "707563939dsf89",
"picture": "htp:// ssdsome data"
},
{
"id": "7075dfsd6393989",
"picture": "htp:// ssdsome data"
},
............
..................... .. and so on ......
내가 JSON 데이터 위의 역 직렬화하는 코드를 시도 :
var parsed = JsonConvert.DeserializeObject<FacebookModel>(data.ToString());
그러나 문제는 rootObject도 변환 할 수없는 몇 가지 JSON 배열을 포함하고있는 JSON 데이터 구조 아래와 같이있다 (비 직렬화). 내 모델 (POCO 클래스)입니다 :
public class FacebookModel
{
public string id { get; set; }
List<AlbumModel> albums { get; set; }
}
public class AlbumModel
{
public string id { get; set; }
public string name { get; set; }
public List<PictureModel> photos { get; set; }
}
public class PictureModel
{
public string id { get; set; }
public string picture { get; set; }
}
오류 : 어떤 도움을 주시면 감사하겠습니다
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Bricks.Common.CustomModels.PictureModel]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
. 감사합니다
간단한 솔루션은 사용자 지정 직렬화를 구현하는 것입니다. –
그래서 json 속성 또는 속성을 사용하여이 문제를 해결할 수 있습니까? –
글쎄, 거기에 오류가, 거기에 속성을 설정하는 말이 맞지? 루트 개체에 배열이 있다고 가정하면 루트 수준의 익명 배열 또는 루트 수준의 이름을 가진 배열에 대해 이야기하고 있습니까? 명명 된 변수는 자체적으로 배열을 deserialize하기 쉽다고 생각합니다. List에서 파생 된 유형으로 JSonObjectAttribute를 클래스에 추가하거나 속성 자체에 속성을 추가하면됩니까? –