2017-12-13 15 views
4

"$ type"이라는 json 속성 이름에 문제가있는 것 같습니다.
이름을 "$ typ"또는 "$ typee"로 변경하면 제대로 작동하는 것 같습니다. 처음에는
나는이 보이지 않는 유니 코드 문자가 있지만 내가 JSON과 Jon Skeet's Unicode Explorer에 속성 값을 모두 복사 - 붙여 넣기 이후 사건이 될 것 같습니다하지 않습니다 나는 이상한 아무것도 볼 수 없습니다 생각

using Newtonsoft.Json; 
using System; 

namespace ConsoleAppCompare 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string json = @"{ 
           ""$type"": ""someText"", 
           ""$someName"": ""MoreText"", 
           ""$ThisWorksToo"": ""en"", 
           ""Counting"": true 
          }"; 

      var movie = JsonConvert.DeserializeObject<Movie>(json); 

      Console.WriteLine("Type:"+ movie.type); //type is null here 
      Console.ReadLine(); 
     } 
    } 

    class Movie 
    { 
     [JsonProperty(PropertyName = "$type")] 
     public string type { get; set; } 

     [JsonProperty(PropertyName = "$someName")] 
     public string Name { get; set; } 

     [JsonProperty(PropertyName = "$ThisWorksToo")] 
     public string Language { get; set; } 

     public bool Counting { get; set; } 
    } 
} 

누구든지 설명이 있습니까? 내가 Newtonsoft.Json.10.0.3

을 사용하고

업데이트 그것은 내가 뭔가를 Newtonsoft.Json에 이름을 예약하고 사용되는

string json = @"{ 
       ""$someName"": ""MoreText"", 
       ""$ThisWorksToo"": ""en"", 
        ""$type"": ""someText"", 
        ""Counting"": true 
       }"; 
+2

나는 명확한 없다,하지만 난'$의 type' 직렬화되는 클래스의 실제 유형을 저장하는 데 사용 newtonsoft에서 예약 된 이름이라고 생각 : 지정 JsonSerializerSettings에 의해 직렬화/역 직렬화를 사용할 수있다. https://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm 여기에서 유형의 전체 이름이 $ type이라는 속성에 저장되는 것을 확인하십시오. 이 충돌을 일으키지 않고 속성 이름을 사용할 수 있도록 newtonsoft를 구성하는 방법을 모르겠습니다. – Will

+2

이 속성은 클래스의 Type 직렬화에 사용되기 때문입니다. – eocron

+0

heh, 어쩌면 틀릴 수도 있습니다 ... – Gabriel

답변

3

다른 somewher 속성을 이동하면 작동하는 것 같군 클래스를 나타내는 Type의 직렬화

new JsonSerializerSettings 
{ 
    TypeNameHandling = TypeNameHandling.All 
} 
+0

'TypeNameHandling.None'을 사용하면 json 속성 이름에서''$ type "'을 사용할 수 있습니까? – Will

+0

@Will 아니오, 그렇지 않습니다. –