Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have mapped data from a webservice to a C# class many times, but I'm stuck on this data source. An object is a list of data within the root object. I have no problems with mapping when a list is inside of an object, but I can't get it when it is a list at the root level. I can access source data in json or xml format.

The json is valid as shown by http://jsonlint.com/

Is this simply something that is not possible in .NET?
At least without using something like this [^]. It seems this was created for this problem.

Sample of data (can't provide link or actual data as it is API data that requires license). Not needed are not needed and not the cause of problems, the "needed" data is failing (I can get first data in series fine, by mapping directly to 2 strings.):
C#
{
    "notneeded1": {
        "sample1": "0",
        "sample2": "2"
    },
    "notneeded2": {
        "sampleA": "1",
        "sampleB": "2"
    },
    "needed": [
        {
            "id": "SymbolA",
            "time": "2012/10/10"
        },
        {
            "id": "SymbolB",
            "time": "2012/10/10"
        },
        {
            "id": "SymbolC",
            "time": "2012/10/10"
        }
        ]
}
Posted
Updated 23-Oct-12 7:20am
v2
Comments
Zoltán Zörgő 23-Oct-12 13:16pm    
Are you using DataContractJsonSerializer?
wizardzz 23-Oct-12 13:24pm    
Thank you for the format fix Marcus

Use FastJSON[^], I am pretty sure it will do the job for you. If the class is unknown, you will get generic collections.
 
Share this answer
 
Comments
wizardzz 23-Oct-12 13:24pm    
I'll reply here instead of the comment. I was using RestSharp and having it automatically deserialize by providing a type to deserialize to. I'm going to try with a generic response from Restsharps's client.execute() now. Thank you, I will check out FastJSON.
SAK, I ended up going a different route. I wrote and tested it, then saw you had answered so I didn't give your version a try, yet.

C#
        public RootObject ExecuteUpdate(RestRequest request, string url)
        {   
            RestClient client = new RestClient();
            client.BaseUrl = url;
            IRestResponse response1 = client.Execute(request);
            string json = response1.Content;
            UpdateObjects.RootObject root = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<rootobject>(json);
            return root;
        }
</rootobject>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900