2014-01-19 2 views
0

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. 

. 감사합니다

+0

간단한 솔루션은 사용자 지정 직렬화를 구현하는 것입니다. –

+0

그래서 json 속성 또는 속성을 사용하여이 문제를 해결할 수 있습니까? –

+0

글쎄, 거기에 오류가, 거기에 속성을 설정하는 말이 맞지? 루트 개체에 배열이 있다고 가정하면 루트 수준의 익명 배열 또는 루트 수준의 이름을 가진 배열에 대해 이야기하고 있습니까? 명명 된 변수는 자체적으로 배열을 deserialize하기 쉽다고 생각합니다. List에서 파생 된 유형으로 JSonObjectAttribute를 클래스에 추가하거나 속성 자체에 속성을 추가하면됩니까? –

답변

4

albumsphotos은 실제로 개체가 아니라 배열입니다. 같은

뭔가 :

public class FacebookModel 
{ 
    public string id { get; set; } 
    public AlbumModel albums { get; set; } 
} 

public class AlbumModel 
{ 
    public List<AlbumData> data {get;set;} 
} 

public class AlbumData 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public PictureModel photos { get; set; } 
} 

public class PictureModel 
{ 
    public List<PictureData> data {get;set;} 
} 

public class PictureData 
{ 
    public string id { get; set; } 
    public string picture { get; set; } 
} 

등등.

-1
이 방법은 복잡한 객체 작동

:

샘플 용도 :
VAR complexObject = GetObjectFromJson <YourComplexObject> (jsonString의 typeof (YourComplexObject));

using System.Runtime.Serialization.Json; 

    public static T GetObjectFromJson<T>(string jsonString, Type type) 
    { 
     var dcSerializer = new DataContractJsonSerializer(type); 
     using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) 
     { 
      return (T)dcSerializer.ReadObject(stream); 
     } 
    } 
+0

질문에 대한 답변이 실제로 없으므로 OP가 deserialize 할 올바른 클래스 구조가 없으므로 OP가 오류가 발생했습니다. 당신의 대답은 그 문제를 해결하는 데 도움이되지 않지만, "나는 deserialize 할 클래스의 정의가 잘되어 있다고 가정 할 때, 그것을 채우기 위해 사용할 수있는 일반적인 방법은 무엇입니까?"라는 다른 질문에 대답합니다. –

관련 문제