2

I'm making calls to an api from asp.net website using http request. The api returns data in JSON.

The request is something like this: GET/stats/{granularity}/{customer_number}/{timestamp_start}/{timestamp_end}.

The problem is that the data returned depends on the timestamps you entered in the request.

This is a sample example:

{
    "success": {
        "1310860635": {
            "1396310400": {
                "iPad": 2317357,
                "Other": 564
            },
            "1396915200": {
                "iPad": 2538835,
                "Other": 372
            },
            "1396656000": {
                "iPad": 2349576,
                "Other": 1136
            },
            "1398729600": {
                "iPad": 1648157,
                "Other": 163,
                "HTC Streaming P": 32
            },
            "1397606400": {
                "iPad": 2018706,
                "Other": 788
            },
            "1396396800": {
                "iPad": 2477197,
                "Other": 608,
                "Spider": 2
            },
            "1397692800": {
                "iPad": 2144772,
                "Other": 576
            },
            "1398816000": {
                "iPad": 2117556,
                "Other": 1838,
                "HTC Streaming P": 14
            },
            "1398124800": {
                "iPad": 2662858,
                "Other": 306,
                "Spider": 3
            },
            "1398038400": {
                "iPad": 2658527,
                "Other": 565,
                "HTC Streaming P": 98,
                "Spider": 1
            },
            "1397260800": {
                "iPad": 1696601,
                "Other": 218
            },
            "1396483200": {
                "iPad": 2431192,
                "Other": 204
            },
            "1398297600": {
                "iPad": 2186146,
                "Other": 567
            },
            "1397001600": {
                "iPad": 330815,
                "Other": 32
            },
            "1398211200": {
                "iPad": 2457731,
                "Other": 381
            },
            "1397347200": {
                "iPad": 2037233,
                "Other": 175
            },
            "1397779200": {
                "iPad": 2438668,
                "Other": 445,
                "HTC Streaming P": 40
            },
            "1396569600": {
                "iPad": 517843,
                "Other": 52,
                "Spider": 1
            },
            "1397433600": {
                "iPad": 1517589,
                "Other": 161
            },
            "1398902400": {
                "iPad": 2059013,
                "Other": 1878
            },
            "1397174400": {
                "iPad": 338428,
                "Other": 57
            },
            "1397520000": {
                "iPad": 2024273,
                "Other": 214
            },
            "1397088000": {
                "iPad": 275725,
                "Other": 21
            },
            "1398384000": {
                "iPad": 2511796,
                "Other": 586
            },
            "1397865600": {
                "iPad": 2585367,
                "Other": 613
            },
            "1398470400": {
                "iPad": 2558398,
                "Other": 327
            },
            "1398556800": {
                "iPad": 1447445,
                "Other": 97
            },
            "1398643200": {
                "iPad": 1475406,
                "Other": 161
            },
            "1396742400": {
                "iPad": 2838708,
                "Other": 484
            },
            "1396828800": {
                "iPad": 2502484,
                "Other": 513
            },
            "1397952000": {
                "iPad": 2724871,
                "Other": 371
            }
        }
    }
}

Is there any way to map this json data to a C# class, which should not depend on timestamp which varies on every request?

4
  • There probably is but you'd better clarify in the structure of the JSON response. In the current form, your question does not really make sense. Commented Jan 20, 2015 at 11:14
  • ok, then tags like "1397952000" represent the dates between StartDate and EndDate in the request. Commented Jan 20, 2015 at 11:17
  • Can you post the Type class of your destination? (e.g. public class IPadClass { public string IPad, public string other }) Commented Jan 20, 2015 at 11:19
  • so the structure is: {"success": {"customer_name": {"timestamp":{"iPad":2317357,"Other":564},... Commented Jan 20, 2015 at 11:20

2 Answers 2

1

Do this.

public class Products
{
    public string ProductName;
    public int Units;
}

Using this class you can serialize like this

// available products is your output
Dictionary<string, List<Products>> availableProducts = new Dictionary<string, List<Products>>();

var serializer = new JavaScriptSerializer();
dynamic result = serializer.Deserialize<dynamic>(json);

KeyValuePair<string, object> temp = ((Dictionary<string, object>)result["success"]).First();

var customerNumber = temp.Key;
var entries = (Dictionary<string, object>)temp.Value;
foreach (var kvp in entries)
{
    var products = new List<Products>();
    IEnumerable collection = (IEnumerable)kvp.Value;
    foreach (dynamic item in collection)
    {
        products.Add(new Products
        {
            ProductName = item.Key,
            Units = item.Value
        });
    }
    availableProducts[kvp.Key] = products;
}

Namespaces used

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;
Sign up to request clarification or add additional context in comments.

3 Comments

"the timestamp can be the key which maps to the list of products retrieved" how can i do that?
is "11310860635" the customer number that you know about previously?
small change here Enumerable.First(result["success"]);
1

//Just a quick snippets

        //Assuming you have this class for your destinationn object
        public class iPadClass
        {
            public string ipad;
            public string other;
        }

        List<iPadClass> ipadList = new List<iPadClass>();

        //since your source is not a typed class
        IEnumerable dataList = getDataFromWebAPI() 

        foreach (var row in dataList)
        {
                ipadList.Add(new iPadClass() { ipad = row[0], other = row[1]});

               //or whichever works for you

                ipadList.Add(new iPadClass() { ipad = row.items[0], other = row.items[1]});
            }
        }

Experiments also on using dataList as List instead of IEnumerable

If you want your source to be also a Typed Class see my answer here regarding such how to convert a JSON structure to another and add a extra field in to it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.