How to deserialize json to multiple types dynamically in c# [duplicate]

This question already has an answer here:

I have a json string like below :

{
“error” : “xxxx” ,
“data” : int type or {…other complex json object}
}

I don’t know when return int or other json string from data field , and how to deserialize this ?

when type of data is object or dynamic , the real deserialized type of data is int or Dictionary using JavaScriptSerializer.Deserialize(string json).

the real deserialized type of data is Int64 or JContainer when using Json.Net.

is there a elegance way to solve this ? I don’t think reflection is a good way.

Deserialize json to anonymous C# object [duplicate]

This question already has an answer here:

I need to deserialize custom JSON to anonymous C# object.

My sample JSON (can be any, even with nested properties as objects):

var json = "{ 'Surname': 'Wick'}"; 

And I want to be deserialized as:

var jsonResult = new { Surname = "Wick" };

But I end up with something like this:

enter image description here

C# get Image Url from JSON response [duplicate]

This question already has an answer here:

I am writing a C# app that is supposed to get event data from JSON

Assuming my JSON response looks like below, how do I get the city_name, image url, date and list of owner names from the response? In JSON below, image could be null and so could be owners.

Also, how would I download the image and show it in my Image control?

{
  "total_items": "24",
  "page_number": "1",
  "page_size": "10",
  "page_count": "3",
  "events": {
    "event": [
      {
        "url": "",
        "id": "event-1",
        "city_name": "Seattle",
        "description": "car show event",
        "image": { <-- THIS COULD BE NULL, HOW TO HANDLE NULL VALUE?
          "thumb": {
            "width": "32",
            "url": "/carshow.jpg",
            "height": "32"
          }
        },
        "date": "2015-12-09 13:20:20",
        "owners": { <-- THIS COULD BE NULL OR MULTIPLE OWNERS, HOW TO GET ALL OWNERS NAMES?
          "owner": [
            {
              "name": "John Doe",
              "id": "O12",
              "bio": "fast track racer"
            },
            {
              "name": "Tom Tomasson",
              "id": "O513",
              "bio": "fines collector"
            }
          ]
        },
      },
      {
        "url": "",
        "id": "event-2",
        "city_name": "Blaine",
        "description": "toyota event",
        "image": null, <-- NO IMAGE IS PROVIDED
        "date": "2015-12-09 13:20:20",
        "owners": null, <-- NO OWNER IS PROVIDED
      },
      {...}
    ]
  }
}

Thanks

How to define a class whose properties change in runtime? [duplicate]

This question already has an answer here:

I have a class called Attributes

public class Attributes
{
    public string Length { get; set; }
    public string Size { get; set; }
    public string Color { get; set; }
    public string Material { get; set; }
    public string Diameter { get; set; }
}

This class is populated from a call to a web service that returns JSON. The web service returns a different set of attributes each time and it is not possible to define all the attributes at compile time as this could run into thousands.

What is the best way to model such a class in c#?

Deserialize array of JSON objects to c# anonymous [duplicate]

This question already has an answer here:

Any ideas how to achieve this?
I have no problem deserializing a single json object like to c# anonymous type

 string json = @"{'Name':'Mike'}";

to

 var definition = new { Name = ""};

with

var result = JsonConvert.DeserializeAnonymousType(json, definition);

but when I have

string jsonArray = @"[{'Name':'Mike'}, {'Name':'Ben'}, {'Name':'Razvigor'}]";

I am stuck.
Thanks!

Can ASP.NET Web API OData Return Non-Collection Properties?

I have the following class:

    public class WeeklyReport
    {
        public IEnumerable DailyReports { get; set; }

        public int? TotalReport
        {
            get
            {
                return DailyReports.Sum(x => x.ReportId);
            }
        }

        public string Title
        {
            get
            {
                return "Weekly Report";
            }
        }
    }

    public class DailyReport
    {
        public string Office { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string ReportTo { get; set; }
        public DateTime Collection { get; set; }
        public int? Leads { get; set; }
    }

In one of my method in OData controller, I return IQueryable of WeeklyReport.
However, when the OData endpoint is queried, the returned JSON looks something like:

{
"odata.metadata": "http://localhost:546/odata/$metadata#WeeklyReports",
"value": [
    {
        "DailyReports": [
            {
                "Office": "002",
                "Name": "First Last",
                "Type": 10,
                "ReportTo": "00002",
                "Collection": "2014-03-18T00:00:00",
                "Leads": null
            },
            {
                "Office": "002",
                "Name": "Agent, ",
                "Type": 10,
                "ReportTo": "00002",
                "Collection": "2014-03-18T00:00:00",
                "Leads": null
            }
        ]
    },
    {
        "DailyReports": [
            {
                "Office": "002",
                "Name": "First Last",
                "Type": 10,
                "ReportTo": "00002",
                "Collection": "2014-03-18T00:00:00",
                "Leads": null
            },
            {
                "Office": "002",
                "Name": "Agent, ",
                "Type": 10,
                "ReportTo": "00002",
                "Collection": "2014-03-18T00:00:00",
                "Leads": null
            }
        ]
    }
]

Is there a way to set ASP.NET Web API OData to return WeeklyReport.TotalReport and WeeklyReport.Title as well?

Thanks!