[CLOSED] JSON deserialize model fields to one nested object

  1. #1

    [CLOSED] JSON deserialize model fields to one nested object

    Hi, I try to find solution for this but unsuccessfully. Her is the problem:

    In model of grid panel I have some model fields:

    fields.Add(Html.X().ModelField().Name("ObracunskiPeriod_ID").Type(ModelFieldType.Int));
                                                fields.Add(Html.X().ModelField().Name("ObracunskiPeriod_Naziv"));
    To controller I send data as json string using parameter:

    Parameter stavke = new Parameter { Name = "pStavke", Value = "Ext.encode(App.GridPanelStavke.getRowsValues())", Mode = ParameterMode.Raw };
    On server I try to deserialize json string to list of objects:

            public ActionResult SacuvajRacun(string p1, string pStavke)
            {
                    List<StavkaDokumenta> _stavke = JSON.Deserialize<List<StavkaDokumenta>>(pStavke);
            }

    I need to deserialize on this way: ObracunskiPeriod_ID and ObracunskiPeriod_Naziv serialize to ObracunskiPeriod object:

        public class ObracunskiPeriod
        {
            [JsonProperty(PropertyName = "ObracunskiPeriod_ID")]
            public int Op_ID { get; set; }
    
            [JsonProperty(PropertyName = "ObracunskiPeriod_Naziv")]
            public string Op_Naziv { get; set; }
         }
    ObracunskiPeriod is one preoperty of StavkaDokumenta:

        public class StavkaDokumenta
        {
            public ObracunskiPeriod Sd_ObracunskiPeriod { get; set; }
        }

    How can I deserialize this?
    Last edited by Daniil; Jul 12, 2012 at 12:26 PM. Reason: [CLOSED]
  2. #2
    Hi,

    You seems to be on the right way suing the JsonProperty attribute.

    The following appears to be working as you need if I correctly understand the requirement.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <%@ Import Namespace="Newtonsoft.Json" %>
    
    <script runat="server">
        class Test
        {
            [JsonProperty(PropertyName = "someProp1")]
            public string Property1 { get; set; }
            [JsonProperty(PropertyName="someProp2")]
            public string Property2 { get; set; }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            string json = "{ someProp1 : '1', someProp2 : '2'}";
    
            Test test = JSON.Deserialize<Test>(json);
    
            X.Msg.Alert("Test", test.Property1 + " " + test.Property2).Show();
        }
    
    </script>
    
    <!DOCTYPE>
    
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
    </head>
    <body>
        <ext:ResourceManager runat="server" />
    </body>
    </html>
  3. #3
    Yes, that is correct, but problem is because I have list where Test class is property. Maybe this example explains my problem:


    class Class1
        {
            public Test MyTest { get; set; }
        }
    
    class Test
        {
            [JsonProperty(PropertyName = "someProp1")]
            public string Property1 { get; set; }
            [JsonProperty(PropertyName="someProp2")]
            public string Property2 { get; set; }
        }
    string json = "[{ someProp1 : '1', someProp2 : '2'}, { someProp1 : '3', someProp2 : '4'}, { someProp1 : '5', someProp2 : '6'}]";
    
    List<Class1> list1 = JSON.Deserialize<List<Class1>>(json);
  4. #4
    Well, this
    List<Class1>
    expects a JSON string like this:
    string json = @"[{
            MyTest : { 
                someProp1 : '1', 
                someProp2 : '2'
            }
        }, {
            MyTest : { 
                someProp1 : '3', 
                someProp2 : '4'
            }]";
    If you can't change the JSON string, then you could deserialize that into
    List<Test>
    Then convert
    List<Test>
    to
    List<Class1>
    using C#. I think it should be possible via lambda expressions.
  5. #5
    I get JSON string from grid panel fields:

    fields.Add(Html.X().ModelField().Name("someProp1"));
    fields.Add(Html.X().ModelField().Name("someProp2"));
    with this code:

    Ext.encode(App.GridPanelStavke.getRowsValues())
    Is any way to chahge model fields structure to give me json string that I expect?
  6. #6
    Quote Originally Posted by boris View Post
    Is any way to chahge model fields structure to give me json string that I expect?
    Only manually I think.

    var values = grid.getRowsValues();
    // convert values to get an appropriate JSON string
  7. #7
    Ok, thanks. I am going to do that manualy.

Similar Threads

  1. [CLOSED] json deserialize problem
    By albayrak in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Jan 21, 2015, 2:28 PM
  2. Replies: 2
    Last Post: May 29, 2011, 3:42 AM
  3. Replies: 4
    Last Post: Feb 01, 2011, 11:54 AM
  4. [CLOSED] JSON.Deserialize Lastes SVN
    By egodoy in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Sep 21, 2010, 5:59 PM
  5. [CLOSED] [1.0] Ext.encode and JSON.Deserialize
    By state in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Apr 14, 2010, 8:03 AM

Tags for this Thread

Posting Permissions