7

Ben benziyor modeli vardır: Ben gönderirken AncakDeserialize modeli C# json verilerini temsil

<input asp-for="Name"> 
<input asp-for="id"> 
<input type="hidden" name="n" value="@JsonConvert.SerializeObject(new Nested())"> 

:

class Nested{ 
    public string Name {get; set;} 
    public int Id {get; set;} 
} 

class Model{ 
    [JsonProperty] 
    public Nested N {get; set;} 
    public string Name {get; set;} 
    public int Id {get; set;} 
} 

ve bunun için bir biçimlendirme böyle bir şeydir Bu form, N alan iki kez kodlanmış gibi görünüyor çünkü, seri hale getirme üzerinde başarısız olur.

var b = JsonConvert.DeserializeAnonymousType(model1, new { N = ""}); 
var c = JsonConvert.DeserializeObject<Nested>(b.N); 

ama bu bir başarısız: Yani bu kod çalışır

var d = JsonConvert.DeserializeAnonymousType(model1, new {N = new Nested()}); 

Ne ihtiyacım o JsonConvert.DeserializeObject<Model>(model1) ile çalışmak yapmaktır. Çalışması için neyi değiştirmeliyim?


örnek:

{"name":"Abc","id":1,"n":"{\"id\":2,\"name\":\"BBB\"}"} 

Aynı sorun this question açıklanan ama teklif değildi, zarif, basit bir çözüm, arıyorum.

+3

JSON serileştirme sorusu örnek JSON verisi olmadan mı? – niksofteng

+0

Örnek olarak ekledim, ancak benzer soruya ve duruma göre, seri hale getirmenin bir durumda çalışıp diğerinde çalışmadığı - json yapısının problem olmadığı açık. Eh, sorun bile zaten tanımlanmış - çözüm tek soru. – Natasha

cevap

2
class Nested{ 
    public string Name {get; set;} 
    public int Id {get; set;} 
} 

class Model{ 
    [JsonProperty] 
    public string N { 
    get { 
     return JsonConverter.DeserializeObject<Nested>(Nested); 
    } 
    set{ 
     Nested = JsonConverter.SerializeObject(value); 
    } 
    } 

    // Use this in your code 
    [JsonIgnore] 
    public Nested Nested {get;set;} 

    public string Name {get; set;} 
    public int Id {get; set;} 
} 
+0

Bu çalışmayı kesin senaryo için yapmanın iyi bir yolu, ancak benzer yapıya sahip her modelde bu kodun gerekmesi, benim için hayal kırıklığına uğramış görünüyor. –

+0

@silent_coder Tek kullanımlık, bu en kolay, aksi halde kesinlikle bir arayüz ve JsonDeserializer yazabilir ve kayıt olabilirsiniz, hepsi bir kez kullanım için çok fazla iş. –

-1

var d = JsonConvert.DeserializeAnonymousType(model1, new {N = new Nested(), 
     Name = "", Id = 0}); 
0

Ben benzer sorunlar vardı ama ters yönde

(nedeniyle EF Proxy ve bu şeyler, uzun bir tarihi kadar) Ama bu olabilir söyleyebilirim deneyin sizin için iyi bir ipucu, ben ConfigureServices yöntemine, benim başlangıçta bu yaptı:

// Add framework services. 
services.AddMvc().AddJsonOptions(options => 
     { 
      // In order to avoid infinite loops when using Include<>() in repository queries 
      options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
     }); 

Ben bunu çözmek için yardımcı olur umarım senin konu.

Juan

0

Eğer bunu otomatik olarak bu serileştirici kullanacağı parametre olarak Modeli sınıfını kullanın zamanlar bu

[Serializable] 
class Model{ 
    [JsonProperty] 
    public Nested N {get; set;} 
    public string Name {get; set;} 
    public int Id {get; set;} 
protected Model(SerializationInfo info, StreamingContext context) 
     { 
      Name = info.GetString("Name"); 
      Id = info.GetInt32("Id"); 
      try { 
      child = (Model)info.GetValue("N", typeof(Model)); 
     } 

     catch (System.Runtime.Serialization.SerializationException ex) 
     { 
      // value N not found 
     } 

     catch (ArgumentNullException ex) 
     { 
      // shouldn't reach here, type or name is null 
     } 

     catch (InvalidCastException ex) 
     { 
      // the value N doesn't match object type in this case (Model) 
     } 
     } 
} 

gibi Runtime.Serialization

şey kullanarak kendi kodu ile kendiniz serialize olabilir biz Sadece yaptım.