Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

using JSON DeserializeAnonymousType method[^]
I managed to get json file using HttpWebRequest.
Everything works just fine for one url/file , but I tried the exaact same code with different anonymous type it gives me the error:
invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access
Please check my code below
Thanks!

What I have tried:

JSON file that works fine:
{
  "makes": [
    {
      "id": "acura",
      "name": "Acura"
    },
    {
      "id": "audi",
      "name": "Audi"
    },
.......

The code to get the data:
string url = "url";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = WebRequestMethods.Http.Get;
httpWebRequest.Accept = "application/json; charset=utf-8";
string JSONfile;
var response = (HttpWebResponse)httpWebRequest.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{ JSONfile = sr.ReadToEnd(); }
var mytb = JsonConvert.DeserializeAnonymousType(JSONfile, new { Makes = default(DataTable) }).Makes;

Another JSON file with almost the same structure:
{
  "vehicle-makes": [
    {
      "id": "acura",
      "name": "Acura",
      "path": "p1"
    },
    {
      "id": "audi",
      "name": "Audi",
      "path": "p2"
    },
......

The code to download the JSON file is the same with the only difference is anonymous type:
string url = "url";
     HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
     httpWebRequest.Method = WebRequestMethods.Http.Get;
     httpWebRequest.Accept = "application/json; charset=utf-8";
     string JSONfile;
     var response = (HttpWebResponse)httpWebRequest.GetResponse();
     using (var sr = new StreamReader(response.GetResponseStream()))
     { JSONfile = sr.ReadToEnd(); }
     var mytb = JsonConvert.DeserializeAnonymousType(JSONfile, new { vehicle-makes = default(DataTable) }).vehicle-makes;

Somehow it doesn't like (vehicle-makes)
Posted
Updated 14-Mar-17 13:28pm
v3
Comments
Graeme_Grant 14-Mar-17 13:43pm    
Any reason you are using Anonymous types? Deserialization will be slower than using defined class data structures...
Samira Radwan 14-Mar-17 14:03pm    
Actually I tried with a class data structure but it returns null.
clsMakes makes = new clsMakes();
makes = JsonConvert.DeserializeObject<clsmakes>(response);
my class looks like:
class clsMakes
{
public string[] id {get;set;}
public string[] name { get; set; }
public string[] path { get; set; }
}
Graeme_Grant 14-Mar-17 14:08pm    
There are many reasons why you are having that problem but it is hard to say without any examples.

I do a lot of work with 3rd-party APIs and this is my favorite tool to generate C# classes from JSON: JSON Utils: Generate C#, VB.Net, SQL Table, Java and PHP from JSON[^]
Samira Radwan 14-Mar-17 14:15pm    
that's really looks very good, Thanks!!
Graeme_Grant 14-Mar-17 14:18pm    
Enjoy! Saved me thousands of hours of work! ;)

vehicle-makes is not a valid object name but vehicle_makes is.
 
Share this answer
 
Comments
Samira Radwan 14-Mar-17 14:01pm    
Thanks a lot this is what I found out!
You can also wrap it in a bracket [], see below as an example.

Object name with dash[^]

JavaScript
var mytb = JsonConvert.DeserializeAnonymousType(JSONfile, new { vehiclemakes = default(DataTable) })["vehicle-makes"];
 
Share this answer
 
Well, looks like DeserializeAnonymousType doesn't like dashes (-) SO I managed to change the anonymous type without dash.
JSONfile = JSONfile.Replace("vehicle-makes", "vehiclemakes");
var mytb = JsonConvert.DeserializeAnonymousType(JSONfile, new { vehiclemakes = default(DataTable) }).vehiclemakes;

Still don't understand why it won't work with (-) in the name but it works now and I have my table

Regards,
Samira
 
Share this answer
 
Comments
Graeme_Grant 14-Mar-17 13:45pm    
Please refer to solution 1 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