2014-04-13 2 views
4

내가 비 직렬화하려는 일관성없는 Json으로 작업하고 있습니다. 문제는 언젠가는 Object를 가지며 때로는 배열이라는 것입니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까? 변환기? 아래는 내 Json 데이터의 스 니펫입니다.Json.net 일관성없는 비 직렬화 JSON

[ 
{ 
"@class": "odd", 
"td": [ 
    { 
    "@class": "user", 
    "@onmouseover": "userInfo('469');", 
    "@onmouseout": "userInfo(0);", 
    "@onmousemove": "moveSlotInfo();", 
    "#text": " AAA" 
    }, 
    { 
    "@id": "day-469-2014-04-07", 
    "@style": "vertical-align: top;", 
    "table": { 
     "@class": "ss", 
     "@cellspacing": "1", 
     "tbody": { 
     "tr": { 
      "td": { 
      "@class": "as", 
      "@style": "color: #ffffff; background-color: #4040ff;", 
      "@onmouseover": "this.className=(document.week_vs_doctor.activityId.value==-1?'sd':'sp');slotInfo('177935',false);", 
      "@onmouseout": "this.className='as';slotInfo(0,false);", 
      "@onmousemove": "moveSlotInfo();", 
      "#text": "KAVAul" 
      } 
     } 
     } 
    } 
    } 
] 
}, 
{ 
"@class": "even", 
"td": [ 
    { 
    "@class": "user", 
    "@onmouseover": "userInfo('262');", 
    "@onmouseout": "userInfo(0);", 
    "@onmousemove": "moveSlotInfo();", 
    "#text": " BBB" 
    }, 
    { 
    "@id": "day-262-2014-04-07", 
    "@style": "vertical-align: top;", 
    "table": { 
     "@class": "ss", 
     "@cellspacing": "1", 
     "tbody": { 
     "tr": [ 
      { 
      "td": { 
       "@class": "as", 
       "@style": "color: #ffffff; background-color: #4040ff;", 
       "@onmouseover": "this.className=(document.week_vs_doctor.activityId.value==-1?'sd':'sp');slotInfo('174318',false);", 
       "@onmouseout": "this.className='as';slotInfo(0,false);", 
       "@onmousemove": "moveSlotInfo();", 
       "#text": "KAVA " 
      } 
      }, 
      { 
      "td": { 
       "@class": "as", 
       "@style": "color: #000000; background-color: #ffc0c0;", 
       "@onmouseover": "this.className=(document.week_vs_doctor.activityId.value==-1?'sd':'sp');slotInfo('174338',false);", 
       "@onmouseout": "this.className='as';slotInfo(0,false);", 
       "@onmousemove": "moveSlotInfo();", 
       "#text": "Dagbak" 
      } 
      } 
     ] 
     } 
    } 
    } 
] 
} 
] 

문제는 tr 객체에 객체 또는 배열이있는 것입니다.

다음은 내 수업 조각

public class Td2 
{ 
    [JsonProperty("@class")] 
    public string TdClass { get; set; } 

    [JsonProperty("@style")] 
    public string style { get; set; } 

    [JsonProperty("@onmouseover")] 
    public string onmouseover { get; set; } 

    [JsonProperty("@onmouseout")] 
    public string onmouseout { get; set; } 

    [JsonProperty("@onmousemove")] 
    public string onmousemove { get; set; } 

    [JsonProperty("#text")] 
    public string text { get; set; } 
} 

public class Tr2 
{ 
    public Td2 td { get; set; } 
} 

public class Tbody2 
{ 
    [JsonProperty] 
    [JsonConverter(typeof(ScheduleJsonConverter<Tr2>))] 
    public List<Tr2> tr { get; set; } 
} 

public class Table 
{ 
    [JsonProperty("@cellspacing")] 
    public string cellspacing { get; set; } 

    public Tbody2 tbody { get; set; } 

    [JsonProperty("@class")] 
    public string tClass { get; set; } 
} 
+0

"불일치"관련 스 니펫 (및/또는 강조 표시)을 포함 시키십시오. 열심히하지 마십시오. – user2864740

+0

나는이 사람이 Tr 요소에 문제가 있다고 가정합니다. JSON에서는 단일 객체를 보여줍니다. 그러나 테이블에는 둘 이상의 행이있을 수 있습니다. – kmacdonald

+0

@ user2864740 Json 스 니펫과 그 아래 텍스트를 보면 "tr 객체에 문제가 있습니다. 객체 또는 배열이 있습니다." 당신은 그것을 열심히하지 않을 것입니다;) – DreamNet

답변

3

어디 선가에서이 변환기를 가지고와 내 프로젝트 중 하나였다입니다. 나는 신용을 제공하고 싶습니다. 나는 그 게시물을 검색하려고 노력할 것이다. 그러나 이렇게하면 문제가 해결됩니다.

EDIT : AH 그것을 발견

JSON.NET Deserialize objects within object/array of objects 단일 객체 또는 객체의 배열 일 수있는 클래스에이 특성을 추가한다. like :

public class Tbody2 
{ 
    [JsonProperty] 
    [JsonConverter(typeof (ObjectToArrayConverter<Tr2>))] 
    public List<Tr2> tr { get; set; } 
} 

JSON이 배열이 아닌 단일 요소 일 경우 변환기는 단일 요소로 배열을 채 웁니다. 그렇지 않은 경우 목록에 deserialize됩니다.

public class ObjectToArrayConverter<T> : CustomCreationConverter<List<T>> where T : new() 
{ 
    public override List<T> Create(Type objectType) 
    { 
     return new List<T>(); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     var target = new List<T>(); 

     try 
     { 
      // Load JObject from stream 
      var jArray = JArray.Load(reader); 

      // Populate the object properties 
      serializer.Populate(jArray.CreateReader(), target); 
     } 
     catch (JsonReaderException) 
     { 
      // Handle case when object is not an array... 

      // Load JObject from stream 
      var jObject = JObject.Load(reader); 

      // Create target object based on JObject 
      var t = new T(); 

      // Populate the object properties 
      serializer.Populate(jObject.CreateReader(), t); 

      target.Add(t); 
     } 

     return target; 
    } 
} 
+2

고마워 형, 그 코드를 변경해야만하고 시도하고 많은 예외를 처리하는 캐치 대신, 나는 사용하는 경우 Reader.TokenType을 사용합니다. 훨씬 더 빠릅니다. 내가 도움을 얻을 수 있도록 내 코드를 게시하고 싶다면 – DreamNet

+0

예. 난 그냥 복사하여 다른 게시물에서 내 프로젝트에 붙여 넣습니다. 나는 내가 그럴 것이라고 생각한다. – kmacdonald

+0

나는 이것이 조금 늦다는 것을 알고 있지만, 여러분과 여러분의 라인을 따르는 모든 사람들에게 축복을 보냅니다! –

관련 문제