[CLOSED] JSON Deserialization Question

  1. #1

    [CLOSED] JSON Deserialization Question

    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?
    Last edited by Daniil; Jan 12, 2015 at 1:57 PM. Reason: [CLOSED]
  2. #2
    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);
  3. #3
    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.

Similar Threads

  1. [CLOSED] Json max length
    By matrixwebtech in forum 2.x Legacy Premium Help
    Replies: 10
    Last Post: Aug 05, 2014, 12:52 PM
  2. [CLOSED] Using JSON.Serialize() and JSON.Deserialize()
    By vadym.f in forum 1.x Legacy Premium Help
    Replies: 7
    Last Post: Apr 30, 2013, 2:37 PM
  3. [CLOSED] Gridpanel Null problem with JSON deserialization
    By IanPearce in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Feb 17, 2011, 4:57 PM
  4. Replies: 1
    Last Post: Aug 31, 2010, 6:33 AM
  5. [CLOSED] [1.0] Need Help to solve JSON Deserialization error
    By Hari_CSC in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Jul 02, 2010, 8:27 PM

Posting Permissions