PDA

View Full Version : [CLOSED] JSON Deserialization Question



cwolcott
Jan 12, 2015, 12:49 AM
I have a Store that use a handler to retrieve data



<ext:Store ID="LiveStatusTWMStore" runat="server" AutoLoad="false">
<Proxy>
<ext:AjaxProxy Url="Handlers/RequestHandler.ashx?mode=LiveStatusByDate">
<Reader>
<ext:JsonReader Root="data" SuccessProperty="success" MessageProperty="message" />
</Reader>
</ext:AjaxProxy>
</Proxy>
...
<Model>
<ext:Model runat="server" IDProperty="RequestStatus">
<Fields>
<ext:ModelField Name="RequestStatus" Type="String" />
<ext:ModelField Name="Today" Type="Int" />
<ext:ModelField Name="Week" Type="Int" />
<ext:ModelField Name="Month" Type="Int" />
<ext:ModelField Name="Quarter" Type="Int" />
<ext:ModelField Name="Year" Type="Int" />
<ext:ModelField Name="Life" Type="Int" />
</Fields>
</ext:Model>
</Model>
</ext:Store>


I have also decided to call the handler from behind to to help build a report. I can call the data a receive the data. The format of the data is:



{"Data" : [{"RequestStatus":"Created", "Today":4,"Week":10, ...},{"RequestStatus":"Opened", "Today":3,"Week":9, ...}]}


What s the best way to deserialize?

Daniil
Jan 12, 2015, 6:22 AM
Hi Chris,

I assume you have a server entity class, say, SomeEntity. I would try this:

List<SomeEntity> data = JSON.Deserialize<List<SomeEntity>>(JSON_string_with_an_array_of_objects);

cwolcott
Jan 12, 2015, 12:30 PM
OK I was able to deserialize and use the object.

The handler prepares the data in the following manner (partial listing), which works perfectly for the Store (shown in previous post) on the client side:



public void ProcessRequest(HttpContext context) {

...
List<object> data = new List<object>();

data.Add( new {RequestStatus = "Created", Today = 5, Week = 7});
data.Add( new {RequestStatus = "Opened", Today = 3, Week = 3});
data.Add( new {RequestStatus = "Unassigned", Today = 1, Week = 2});

new StoreResponsseData {
Data = JSON.Serialize(data),
}.Return();
}


The data being returned looks like the following


{"Data" : [{"RequestStatus":"Created", "Today":5,"Week":7},{"RequestStatus":"Opened", "Today":3,"Week":3},{"RequestStatus":"Unassigned", "Today":1,"Week":2}]}


I hand not previously defined a server entity class, because the data was only used by a store. So I defined:


public class LiveStatus_Periods
{
[JsonProperty(PropertyName = "RequestStatus")]
public string RequestStatus {get; set;}
[JsonProperty(PropertyName = "Today")]
public int Today {get; set;}
[JsonProperty(PropertyName = "Week")]
public int Week {get; set;}
}


I then deserialized by:


Dictionary<string, List<LiveStatus_Periods>> data = JSON.Deserialize<Dictionary<string, List<LiveStatus_Periods>>>(json);
List<LiveStatus_Periods> periods = data["data"];


I can now use the data. Please close the thread.