PDA

View Full Version : [CLOSED] JSON deserialize model fields to one nested object



boris
Jul 11, 2012, 5:18 PM
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?

Daniil
Jul 12, 2012, 9:30 AM
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>

boris
Jul 12, 2012, 10:01 AM
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);

Daniil
Jul 12, 2012, 10:10 AM
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.

boris
Jul 12, 2012, 10:18 AM
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?

Daniil
Jul 12, 2012, 10:23 AM
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

boris
Jul 12, 2012, 10:51 AM
Ok, thanks. I am going to do that manualy.