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)