hey i want to Deserialize this json API response to get values including profile state etc for processing in the program.
i tried multiple ways from different questions in here but i get response as null.
here is the code, correct me what i am doing wrong please
{
"response": {
"players": [{
"steamid": "xxxxxxxxxxxxxxxxx",
"communityvisibilitystate": 3,
"profilestate": 1,
"personaname": "xxxx xxxx",
"lastlogoff": 1529478555,
"commentpermission": 1,
"profileurl": "xxxxxxxxxxxxxxx",
"avatar": "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"avatarmedium": "xxxxxxxxxxxxxxxxxxxxx",
"avatarfull": "xxxxxxxxxxx",
"personastate": 1,
"realname": "xxxx",
"primaryclanid": "xxxxxxxxxx",
"timecreated": 1097464215,
"personastateflags": 0
}]
}
}
The code i tried
public class AccountInfo
{
public string steamid { get; set; }
public int communityvisibilitystate { get; set; }
public int profilestate { get; set; }
public string personaname { get; set; }
public ulong lastlogoff { get; set; }
public int commentpermission { get; set; }
public string profileurl { get; set; }
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public int personastate { get; set; }
public string realname { get; set; }
public string primaryclanid { get; set; }
public ulong timecreated { get; set; }
public int personastateflags { get; set; }
}
public class Response
{
public AccountInfo response { get; set; }
}
public class Response1
{
public Response players { get; set; }
}
static void Main(string[] args)
{
DeserilizeJson();
}
internal static void DeserilizeJson()
{
string json = GetUrlToString("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
Console.WriteLine(json);
Response1 info = JsonConvert.DeserializeObject<Response1>(json);
using (StreamWriter file = File.CreateText(#"c:\test.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, info);
}
}
internal static string GetUrlToString(string url)
{
String Response = null;
try
{
using (WebClient client = new WebClient())
{
Response = client.DownloadString(url);
}
}
catch (Exception)
{
return null;
}
return Response;
}
Use this as a Model Class
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class JsonModel
{
[JsonProperty("response")]
public Response Response { get; set; }
}
public partial class Response
{
[JsonProperty("players")]
public List<Player> Players { get; set; }
}
public partial class Player
{
[JsonProperty("steamid")]
public string Steamid { get; set; }
[JsonProperty("communityvisibilitystate")]
public long Communityvisibilitystate { get; set; }
[JsonProperty("profilestate")]
public long Profilestate { get; set; }
[JsonProperty("personaname")]
public string Personaname { get; set; }
[JsonProperty("lastlogoff")]
public long Lastlogoff { get; set; }
[JsonProperty("commentpermission")]
public long Commentpermission { get; set; }
[JsonProperty("profileurl")]
public string Profileurl { get; set; }
[JsonProperty("avatar")]
public string Avatar { get; set; }
[JsonProperty("avatarmedium")]
public string Avatarmedium { get; set; }
[JsonProperty("avatarfull")]
public string Avatarfull { get; set; }
[JsonProperty("personastate")]
public long Personastate { get; set; }
[JsonProperty("realname")]
public string Realname { get; set; }
[JsonProperty("primaryclanid")]
public string Primaryclanid { get; set; }
[JsonProperty("timecreated")]
public long Timecreated { get; set; }
[JsonProperty("personastateflags")]
public long Personastateflags { get; set; }
}
Then do this in your Main Class
var info = JsonConvert.DeserializeObject<JsonModel>(json);
var Response = info.Response
Open nuget, search newtonsoft.json and install.
Deserialize:
var deserialized = JsonConvert.DeserializeObject(jsonstring);
Try this:
public class Player
{
public string steamid { get; set; }
public int communityvisibilitystate { get; set; }
public int profilestate { get; set; }
public string personaname { get; set; }
public ulong lastlogoff { get; set; }
public int commentpermission { get; set; }
public string profileurl { get; set; }
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public int personastate { get; set; }
public string realname { get; set; }
public string primaryclanid { get; set; }
public ulong timecreated { get; set; }
public int personastateflags { get; set; }
}
public class Response
{
public Player[] players { get; set; }
}
public class EncapsulatedResponse
{
public Response response {get;set;}
}
internal static void DeserilizeJson()
{
string json = GetUrlToString("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=xxxxxxxxxxxxxxxxxxxxx&steamids=xxxxxxxxxxxxxxx");
Console.WriteLine(json);
EncapsulatedResponse info = JsonConvert.DeserializeObject<EncapsulatedResponse>(json);
using (StreamWriter file = File.CreateText(#"c:\test.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, info);
}
}
The players property should be a list of players as it is an array . Below is the Correct Model
public class Player
{
public string steamid { get; set; }
public int communityvisibilitystate { get; set; }
public int profilestate { get; set; }
public string personaname { get; set; }
public int lastlogoff { get; set; }
public int commentpermission { get; set; }
public string profileurl { get; set; }
public string avatar { get; set; }
public string avatarmedium { get; set; }
public string avatarfull { get; set; }
public int personastate { get; set; }
public string realname { get; set; }
public string primaryclanid { get; set; }
public int timecreated { get; set; }
public int personastateflags { get; set; }
}
public class Response
{
public List<Player> players { get; set; }
}
You need to change the object structure:
public class Response
{
public AccountInfo[] players { get; set; }
}
public class Response1
{
public Response response { get; set; }
}
then deserialize Response1 (like u do currently)
Just to provide a different approach, you can use JObject (Newtonsoft.Json.Linq) so that you only need the AccountInfo class:
var accounts = JObject.Parse(json).Root
.SelectToken("response.players")
.ToObject(typeof(AccountInfo[]));
Or in some cases, it is even easier just navigating the properties:
var accounts = JObject.Parse(json)["response"]["players"]
.ToObject((typeof(AccountInfo[])));
I am using rest api to get a list of places from a given location using google maps but i'm having some issues .
Main Issue : While the response is ok most of the time , sometimes i get a wrongly formatted json or status="Not Ok" which cause my app to crash :
Is there a way to test for the status & validity of the json before deserializing to prevent crashes or other way ?
System.NullReferenceException: Object reference not set to an instance of an object.
from this line :
place = JsonConvert.DeserializeObject<Places>(content,settings);
Secondary issue : I'm not sure what is the best way to deal with the page token returned and how to use it for recurring queries
Code for getting the json ( The try-catch does nothing - app still crashes)
public async Task<Places> recurNearbyPlacesRequest(int radius , string pageToken)
{
HttpClient client = new HttpClient();
Places place = null;
string testurl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=45.498358,-73.4721847&radius=2000&pagetoken={0}&key={1}";
var uri = new Uri(string.Format(testurl, pageToken , Constants.google_maps_key));
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
try
{
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
place = JsonConvert.DeserializeObject<Places>(content,settings);
}
}
catch (Exception E) { }
return place;
}
Error Json that causes crash :
{
"error_message" : "The provided API key is invalid.",
"html_attributions" : [],
"results" : [],
"status" : "REQUEST_DENIED"
}
{
"html_attributions" : [],
"results" : [],
"status" : "INVALID_REQUEST"
}
Places Class :
//JsontoC#
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Geometry
{
public Location location { get; set; }
public Viewport viewport { get; set; }
}
public class OpeningHours
{
public bool open_now { get; set; }
public List<object> weekday_text { get; set; }
}
public class Photo
{
public int height { get; set; }
public List<string> html_attributions { get; set; }
public string photo_reference { get; set; }
public int width { get; set; }
}
public class Result
{
public Geometry geometry { get; set; }
public string icon { get; set; }
public string id { get; set; }
public string name { get; set; }
public OpeningHours opening_hours { get; set; }
public List<Photo> photos { get; set; }
public string place_id { get; set; }
public double rating { get; set; }
public string reference { get; set; }
public string scope { get; set; }
public List<string> types { get; set; }
public string vicinity { get; set; }
public int? price_level { get; set; }
}
public class RootObject
{
public List<object> html_attributions { get; set; }
public string next_page_token { get; set; }
public List<Result> results { get; set; }
public string status { get; set; }
}
I am trying to parse a whois json response but when I try parse it I get null values.
string html;
string whoisUrl = "https://whois.apitruck.com/:google.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(whoisUrl);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
html = reader.ReadToEnd();
}
}
Class1 m = JsonConvert.DeserializeObject<Class1>(html);
MessageBox.Show(m.created);
Object
class Class1
{
public string created { get; set; }
}
can anyone please point what I am doing wrong here ?
Your Class1 doesn't get the value since "created" is part of the "response" and not the root level of the JSON reponse.
You'll either need to use dynamic or create a hierarchy for the classes for a simple fix.
class Class1
{
public Response Response { get; set; }
}
class Response
{
public string created { get; set; }
}
Then you can use this:
Class1 m = JsonConvert.DeserializeObject<Class1>(html);
MessageBox.Show(m.Response.created);
UPDATE
Also, here's an example of how to use the dynamic:
var m = JsonConvert.DeserializeObject<dynamic>(html);
DateTime created = (DateTime)m.response.created;
There is nice app to convert json to .net class:
public class Registrar
{
public string id { get; set; }
public string name { get; set; }
public object email { get; set; }
public string url { get; set; }
}
public class Response
{
public string name { get; set; }
public string idnName { get; set; }
public List<string> status { get; set; }
public List<string> nameserver { get; set; }
public object ips { get; set; }
public string created { get; set; }
public string changed { get; set; }
public string expires { get; set; }
public bool registered { get; set; }
public bool dnssec { get; set; }
public string whoisserver { get; set; }
public List<object> contacts { get; set; }
public Registrar registrar { get; set; }
public List<string> rawdata { get; set; }
public object network { get; set; }
public object exception { get; set; }
public bool parsedContacts { get; set; }
}
public class RootObject
{
public int error { get; set; }
public Response response { get; set; }
}
...
RootObject result = JsonConvert.DeserializeObject<RootObject>(html);
var created = result.response.created;
I'm deserializing from a json file , which has the following fields :
{ 'IdTechnician': '4', 'DescTechnician': 'Surname Name', 'LoginTechnician': 'username', 'TypeTechnician': '1', 'TelephoneTechnician': '+123456789', 'SignatureTechnician': '????\\u0000\\u0010JFIF\\u0000\\u0001\\u0002\\u0001\\u0001,\\u0001,\\u0000\\u0000??\\u0000\\u000eAdobe\\u0000d\\u0000\\u0000\\u0000\\u0000\\u0001??\\t?Exif\\u0000\\u0000MM\\u0000*\\u0000\\u0000\\u0000\\b\\u0000\\u0007\\u0001\\u0012\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001\\u0000\\u0001\\u0000\\u0000\\u0001\\u001a\\u0000\\u0005\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000b\\u0001\\u001b\\u0000\\u0005\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000j\\u0001(\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0000\\u0000\\u00011\\u0000\\u0002\\u0000\\u0000\\u0000\\u001c\\u0000\\u0000\\u0000r\\u00012\\u0000\\u0002\\u0000\\u0000\\u0000\\u0014\\u0000\\u0000\\u0000??i\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000?\\u0001,\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0001,\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000Adobe Photoshop CS4 Windows\\u00002011:03:09 17:10:23\\u0000\\u0000\\u0003?\\u0001\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001??\\u0000\\u0000?\\u0002\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000??\\u0003\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0006\\u0001\\u0003\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001\\u0000\\u0006\\u0000\\u0000\\u0001\\u001a\\u0000\\u0005\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0001\\u001c\\u0001\\u001b\\u0000\\u0005\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0001$\\u0001(\\u0000\\u0003\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0000\\u0000\\u0002\\u0001\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0001,\\u0002\\u0002\\u0000\\u0004\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\b^\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000H\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000H\\u0000\\u0000\\u0000\\u0001????\\u0000\\u0010JFIF\\u0000\\u0001\\u0002\\u0000\\u0000H\\u0000H\\u0000\\u0000??\\u0000\\fAdobe_CM\\u0000\\u0002??\\u0000\\u000eAdobe\\u0000d?\\u0000\\u0000\\u0000\\u0001??\\u0000?\\u0000\\f\\b\\b\\b\\t\\b\\f\\t\\t\\f\\u0011\\u000b\\n\\u000b\\u0011\\u0015\\u000f\\f\\f\\u000f\\u0015\\u0018\\u0013\\u0013\\u0015\\u0013\\u0013\\u0018\\u0011\\f\\f\\f\\f\\f\\f\\u0011\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\u0001\\r\\u000b\\u000b\\r\\u000e\\r\\u0010\\u000e\\u000e\\u0010\\u0014\\u000e\\u000e\\u000e\\u0014\\u0014\\u000e\\u000e\\u000e\\u000e\\u0014\\u0011\\f\\f\\f\\f\\f\\u0011\\u0011\\f\\f\\f\\f\\f\\f\\u0011\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f??\\u0000\\u0011\\b\\u0000$\\u0000?\\u0003\\u0001\\\\u0000\\u0002\\u0011\\u0001\\u0003\\u0011\\u0001??\\u0000\\u0004\\u0000\\n??\\u0001?\\u0000\\u0000\\u0001\\u0005\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0003\\u0000\\u0001\\u0002\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\u0001\\u0000\\u0001\\u0005\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\u0010\\u0000\\u0001\\u0004\\u0001\\u0003\\u0002\\u0004\\u0002\\u0005\\u0007\\u0006\\b\\u0005\\u0003\\f3\\u0001\\u0000\\u0002\\u0011\\u0003\\u0004!\\u00121\\u0005AQa\\u0013\\q?2\\u0006\\u0014???B#$\\u0015R?b34r??C\\u0007%?S???cs5\\u0016???&D?TdE??t6\\u0017?U?e?????u??F'???????????????Vfv????????7GWgw????????\\u0011\\u0000\\u0002\\u0002\\u0001\\u0002\\u0004\\u0004\\u0003\\u0004\\u0005\\u0006\\u0007\\u0007\\u0006\\u00055\\u0001\\u0000\\u0002\\u0011\\u0003!1\\u0012\\u0004AQaq\\\\u0013\\u00052??\\u0014??B#?R??3$b?r??CS\\u0015cs4?%\\u0006\\u0016???\\u0007&5??D?T?\\u0017dEU6te??????u??F???????????????Vfv????????'7GWgw???????\\u0000\\f\\u0003\\u0001\\u0000\\u0002\\u0011\\u0003\\u0011\\u0000?\\u0000?E[??;\\u0013\\u0006??????Z?0]?????????3?m?t?f}??/????????????:?????\\u0000K?\\u0000\\u0017_?\\u0011\\u0002?????F5.??\\u001b]L??05???<l??qq?}4???C^??\\u0000\\u0011???\\u0000?{?\\u0000??oC?e?2???+7\\r??&??[?>??z~?^?[-?}??V:??44PQ \\t&\\u0002`A\\u0012\\f??\\n?X???j\\u001c???????\\u0016???w?1??c?????n,????\\u0002\\u0012?-M??I\\u0005)$?IJU?3YM???n?sK?C#v????;k*g??V\\u001e????!?h?\\u0013?\\u0003RVN\\u0006\\u0016]????h??h????\\u0000\\u001fv;\\u0019??;\\u001a?z_???z^?!N?6Krk?\\u001a?0?\\u0016=???sN?5?-EB???\\u001a??^????\\u001d?q's??\\u001f????z*\\nRI$???I???nq\\r\\u0003?t\\t)t?+??G?E??4?\\u000b}F????,^???p?\\u0019???\\u0013\\'?\\u00034?\\u0000?s\\u0005_???=?????N??y????????\\\\?y?io?????w;??c?\\u0000?~????j?X??f??c?1???~?I?????\\u001c??,?~S???\\u0000???GcfU?fN3j????{ls???{?5?1?n???z~??}??\\u001e]??????????K^????\\u001b??????k?~E??{??P?l???\\t>?P?p?\\u0005?\\u0000??\\u0000?????\\u0000a\\u00133??\\u0002?sl??,?m/u??\\\\??????w?w?????r????r???????,]_??\\u0003?o?\\r???cY?e?~?u?Mv^5?\\u001a?j?c\\u0000?\\u0017?m?\\u00007eh???\\u0000*S??:?3?sZ?T???n\\u0000\\u001a?w?{\\u0018????????B??J?\\u0000?10?v3?w??????$??V??=??W?mX?Q???????;k=,Ue??X??f&?B?=????????F???g?[n^NmtdWS????????h?????\\u001a=OK??????}???\\u0011?hv?[?beU?Cn?\\u0010\\f?1?\\u001c?4???\\u001b??V?\\u0000c??????\\u001bE???\\u001d?\\u000688?????EsN????uXT_????vU-?O?X{\\u001df\\u0007?\\u001f??n???>?]?????=\\u001d??Tu*\\u001d??n\\u0016\\u000eP??sK??YT??k???{(k~????????\\u0003\\u001e???H\\u0010D?A???C?g???b?<????<~??,?R?????\\u0000??????\\rh\\rh\\u0010\\u0000?\\u0000\\u0013i-.??7?f?\\u0018>??Gii\\u0005??V?e\\u0014\\n??6??+?\\u0012\\u0004?=??~???Oclc?x\\u000ec?k?u\\u0004\\u001d\\u001c????D?b??k????W?V????????hs[^???c???g??\\u0000??\\u0014tS?????ev5????i\\u0004???\\u0003??m???\\\\<zQ?|?c??\\u0015????????v\\u0015\\u0015T?dd>?c?,??\\u001b\\u001d?6?[n???Z?G\\u001f?\\'??????~;???s?ln77y8?WX;??\\u001a?1?no???Uz??J?n????\\\\`\\rI<\\u0000?r???W?\\u0017d??5\\u000e\\ro??\\u00077??b?K?\\u0019?\\u0017?? ?????h,ap\\u0011?}?????????tG?ul??????-}/c\\u001c???\\u001f??\\u0014?kk~U???}??c???[?\\'??L?Z???T?\\u00167??ac\\u001cX????]??ev;??6????Q?W??>????#7WV???\\b??O??????\\u0016>???m?\\u001c?;!???????q?\\u0011?\\u001a?+??\\u0001??L????\\u001a?S?\\u0015h????X??,?e~??????h?\\u000e?????????\\u0000????Z\\u0003?\\u0015^\\r?*0[K-?????\\u0005????\\u001f??mk??????t??Z???h??k???gc\\bkv???#??]g??V?\\u000e????D???$??????$??????$?????? ??????\\'??????$??????$??????$??????$??????$?????\\u000e8Photoshop 3.0\\u00008BIM\\u0004%\\u0000\\u0000\\u0000\\u0000\\u0000\\u0010\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u00008BIM\\u0003?\\u0000\\u0000\\u0000\\u0000\\u0000\\u0010\\u0001,\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0001,\\u0000\\u0000\\u0000\\u0001\\u0000\\u00028BIM\\u0004&\\u0000\\u0000\\u0000\\u0000\\u0000\\u000e\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000??\\u0000\\u00008BIM\\u0004\\r\\u0000\\u0000\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u001e8BIM\\u0004\\u0019\\u0000\\u0000\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u001e8BIM\\u0003?\\u0000\\u0000\\u0000\\u0000\\u0000\\t\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u00008BIM'\\u0010\\u0000\\u0000\\u0000\\u0000\\u0000\\n\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u00028BIM\\u0003?\\u0000\\u0000\\u0000\\u0000\\u0000H\\u0000/ff\\u0000\\u0001\\u0000lff\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000/ff\\u0000\\u0001\\u0000???\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u00002\\u0000\\u0000\\u0000\\u0001\\u0000Z\\u0000\\u0000\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u00005\\u0000\\u0000\\u0000\\u0001\\u0000-\\u0000\\u0000\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u00018BIM\\u0003?\\u0000\\u0000\\u0000\\u0000\\u0000p\\u0000\\u0000??????????????????????\\u0003?\\u0000\\u0000\\u0000\\u0000??????????????????????\\u0003?\\u0000\\u0000\\u0000\\u0000??????????????????????\\u0003?\\u0000\\u0000\\u0000\\u0000??????????????????????\\u0003?\\u0000\\u00008BIM\\u0004\\b\\u0000\\u0000\\u0000\\u0000\\u0000\\u0010\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0002#\\u0000\\u0000\\u0002#\\u0000\\u0000\\u0000\\u00008BIM\\u0004\\u001e\\u0000\\u0000\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u00008BIM\\u0004\\u001a\\u0000\\u0000\\u0000\\u0000\\u0003O\\u0000\\u0000\\u0000\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000\\r\\u0000c\\u0000o\\u0000r\\u0000s\\u0000i\\u0000n\\u0000i\\u0000a\\u0000n\\u0000d\\u0000r\\u0000e\\u0000a\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0010\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000null\\u0000\\u0000\\u0000\\u0002\\u0000\\u0000\\u0000\\u0006boundsObjc\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Rct1\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u0000Top long\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Leftlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Btomlong\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000\\u0000Rghtlong\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000\\u0006slicesVlLs\\u0000\\u0000\\u0000\\u0001Objc\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0005slice\\u0000\\u0000\\u0000\\u0012\\u0000\\u0000\\u0000\\u0007sliceIDlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0007groupIDlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0006originenum\\u0000\\u0000\\u0000\\fESliceOrigin\\u0000\\u0000\\u0000\\rautoGenerated\\u0000\\u0000\\u0000\\u0000Typeenum\\u0000\\u0000\\u0000\\nESliceType\\u0000\\u0000\\u0000\\u0000Img \\u0000\\u0000\\u0000\\u0006boundsObjc\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Rct1\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u0000Top long\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Leftlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000Btomlong\\u0000\\u0000\\u0000-\\u0000\\u0000\\u0000\\u0000Rghtlong\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000\\u0003urlTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000nullTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000MsgeTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0006altTagTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u000ecellTextIsHTMLbool\\u0001\\u0000\\u0000\\u0000\\bcellTextTEXT\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\thorzAlignenum\\u0000\\u0000\\u0000\\u000fESliceHorzAlign\\u0000\\u0000\\u0000\\u0007default\\u0000\\u0000\\u0000\\tvertAlignenum\\u0000\\u0000\\u0000\\u000fESliceVertAlign\\u0000\\u0000\\u0000\\u0007default\\u0000\\u0000\\u0000\\u000bbgColorTypeenum\\u0000\\u0000\\u0000\\u0011ESliceBGColorType\\u0000\\u0000\\u0000\\u0000None\\u0000\\u0000\\u0000\\ttopOutsetlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\nleftOutsetlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\fbottomOutsetlong\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u000brightOutsetlong\\u0000\\u0000\\u0000\\u0000\\u00008BIM\\u0004(\\u0000\\u0000\\u0000\\u0000\\u0000\\f\\u0000\\u0000\\u0000\\u0002??\\u0000\\u0000\\u0000\\u0000\\u0000\\u00008BIM\\u0004\\u0011\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0001\\u00008BIM\\u0004\\u0014\\u0000\\u0000\\u0000\\u0000\\u0000\\u0004\\u0000\\u0000\\u0000\\u00018BIM\\u0004\\f\\u0000\\u0000\\u0000\\u0000\\bz\\u0000\\u0000\\u0000\\u0001\\u0000\\u0000\\u0000?\\u0000\\u0000\\u0000$\\u0000\\u0000\\u0001?\\u0000\\u0000C?\\u0000\\u0000\\b^\\u0000\\u0018\\u0000\\u0001????\\u0000\\u0010JFIF\\u0000\\u0001\\u0002\\u0000\\u0000H\\u0000H\\u0000\\u0000??\\u0000\\fAdobe_CM\\u0000\\u0002??\\u0000\\u000eAdobe\\u0000d?\\u0000\\u0000\\u0000\\u0001??\\u0000?\\u0000\\f\\b\\b\\b\\t\\b\\f\\t\\t\\f\\u0011\\u000b\\n\\u000b\\u0011\\u0015\\u000f\\f\\f\\u000f\\u0015\\u0018\\u0013\\u0013\\u0015\\u0013\\u0013\\u0018\\u0011\\f\\f\\f\\f\\f\\f\\u0011\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\u0001\\r\\u000b\\u000b\\r\\u000e\\r\\u0010\\u000e\\u000e\\u0010\\u0014\\u000e\\u000e\\u000e\\u0014\\u0014\\u000e\\u000e\\u000e\\u000e\\u0014\\u0011\\f\\f\\f\\f\\f\\u0011\\u0011\\f\\f\\f\\f\\f\\f\\u0011\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f\\f??\\u0000\\u0011\\b\\u0000$\\u0000?\\u0003\\u0001\\\\u0000\\u0002\\u0011\\u0001\\u0003\\u0011\\u0001??\\u0000\\u0004\\u0000\\n??\\u0001?\\u0000\\u0000\\u0001\\u0005\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0003\\u0000\\u0001\\u0002\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\u0001\\u0000\\u0001\\u0005\\u0001\\u0001\\u0001\\u0001\\u0001\\u0001\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0000\\u0001\\u0000\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\u0010\\u0000\\u0001\\u0004\\u0001\\u0003\\u0002\\u0004\\u0002\\u0005\\u0007\\u0006\\b\\u0005\\u0003\\f3\\u0001\\u0000\\u0002\\u0011\\u0003\\u0004!\\u00121\\u0005AQa\\u0013\\'q?2\\u0006\\u0014???B#$\\u0015R?b34r??C\\u0007%?S???cs5\\u0016???&D?TdE??t6\\u0017?U?e?????u??F'???????????????Vfv????????7GWgw????????\\u0011\\u0000\\u0002\\u0002\\u0001\\u0002\\u0004\\u0004\\u0003\\u0004\\u0005\\u0006\\u0007\\u0007\\u0006\\u00055\\u0001\\u0000\\u0002\\u0011\\u0003!1\\u0012\\u0004AQaq\\\\u0013\\u00052??\\u0014??B#?R??3$b?r??CS\\u0015cs4?%\\u0006\\u0016???\\u0007&5??D?T?\\u0017dEU6te??????u??F???????????????Vfv????????'7GWgw???????\\u0000\\f\\u0003\\u0001\\u0000\\u0002\\u0011\\u0003\\u0011\\u0000?\\u0000?E[??;\\u0013\\u0006??????Z?0]?????????3?m?t?f}??/????????????:?????\\u0000K?\\u0000\\u0017_?\\u0011\\u0002?????F5.??\\u001b]L??05???<l??qq?}4???C^??\\u0000\\u0011???\\u0000?{?\\u0000??oC?e?2???+7\\r??&??[?>??z~?^?[-?}??V:??44PQ \\t&\\u0002`A\\u0012\\f??\\n?X???j\\u001c???????\\u0016???w?1??c?????n,????\\u0002\\u0012?-M??I\\u0005)$?IJU?3YM???n?sK?C#v????;k*g??V\\u001e????!?h?\\u0013?\\u0003RVN\\u0006\\u0016]????h??h????\\u0000\\u001fv;\\u0019??;\\u001a?z_???z^?!N?6Krk?\\u001a?0?\\u0016=???sN?5?-EB???\\u001a??^????\\u001d?q's??\\u001f????z*\\nRI$???I???nq\\r\\u0003?t\\t)t?+??G?E??4?\\u000b}F????,^???p?\\u0019???\\u0013\\'?\\u00034?\\u0000?s\\u0005_???=?????N??y????????\\\\?y?io?????w;??c?\\u0000?~????j?X??f??c?1???~?I?????\\u001c??,?~S???\\u0000???GcfU?fN3j????{ls???{?5?1?n???z~??}??\\u001e]??????????K^????\\u001b??????k?~E??{??P?l???\\t>?P?p?\\u0005?\\u0000??\\u0000?????\\u0000a\\u00133??\\u0002?sl??,?m/u??\\\\??????w?w?????r????r???????,]_??\\u0003?o?\\r???cY?e?~?u?Mv^5?\\u001a?j?c\\u0000?\\u0017?m?\\u00007eh???\\u0000*S??:?3?sZ?T???n\\u0000\\u001a?w?{\\u0018????????B??J?\\u0000?10?v3?w??????$??V??=??W?mX?Q???????;k=,Ue??X??f&?B?=????????F???g?[n^NmtdWS????????h?????\\u001a=OK??????}???\\u0011?hv?[?beU?Cn?\\u0010\\f?1?\\u001c?4???\\u001b??V?\\u0000c??????\\u001bE???\\u001d?\\u000688?????EsN????uXT_????vU-?O?X{\\u001df\\u0007?\\u001f??n???>?]?????=\\u001d??Tu*\\u001d??n\\u0016\\u000eP??sK??YT??k???{(k~????????\\u0003\\u001e???H\\u0010D?A???C?g???b?<????<~??,?R?????\\u0000??????\\rh\\rh\\u0010\\u0000?\\u0000\\u0013i-.??7?f?\\u0018>??Gii\\u0005??V?e\\u0014\\n??6??+?\\u0012\\u0004?=??~???Oclc?x\\u000ec?k?u\\u0004\\u001d\\u001c????D?b??k????W?V????????hs[^???c???g??\\u0000??\\u0014tS?????ev5????i\\u0004???\\u0003??m???\\\\<zQ?|?c??\\u0015????????v\\u0015\\u0015T?dd>?c?,??\\u001b\\u001d?6?[n???Z?G\\u001f?\\'??????~;???s?ln77y8?WX;??\\u001a?1?no???Uz??J?n????\\\\`\\rI<\\u0000?r???W?\\u0017d??5\\u000e\\ro??\\u00077??b?K?\\u0019?\\u0017?? ?????h,ap\\u0011?}?????????tG?ul??????-}/c\\u001c???\\u001f??\\u0014?kk~U???}??c???[?\\'??L?Z???T?\\u00167??ac\\u001cX????]??ev;??6????Q?W??>????#7WV???\\b??O??????\\u0016>???m?\\u001c?;!???????q?\\u0011?\\u001a?+??\\u0001??L????\\u001a?S?\\u0015h????X??,?e~??????h?\\u000e?????????\\u0000????Z\\u0003?\\u0015^\\r?*0[K-?????\\u0005????\\u001f??mk??????t??Z???h??k???gc\\bkv???#??]g??V?\\u000e????D???$??????$??????$?????? ??????\\'??????$??????$??????$??????$??????$???8BIM\\u0004!\\u0000\\u0000\\u0000\\u0000\\u0000U\\u0000\\u0000\\u0000\\u0001\\u0001\\u0000\\u0000\\u0000\\u000f\\u0000A\\u0000d\\u0000o\\u0000b\\u0000e\\u0000 \\u0000P\\u0000h\\u0000o\\u0000t\\u0000o\\u0000s\\u0000h\\u0000o\\u0000p\\u0000\\u0000\\u0000\\u0013\\u0000A\\u0000d\\u0000o\\u0000b\\u0000e\\u0000 \\u0000P\\u0000h\\u0000o\\u0000t\\u0000o\\u0000s\\u0000h\\u0000o\\u0000p\\u0000 \\u0000C\\u0000S\\u00004\\u0000\\u0000\\u0000\\u0001\\u00008BIM\\u0004\\u0006\\u0000\\u0000\\u0000\\u0000\\u0000\\u0007\\u0000\\b\\u0000\\u0000\\u0000\\u0001\\u0001\\u0000??\\u0010Chttp://ns.adobe.com/xap/1.0/\\u0000<?xpacket begin=\\'???\\' id=\\'W5M0MpCehiHzreSzNTczkc9d\\'?>\\r\\n<x:xmpmeta xmlns:x=\\'adobe:ns:meta/\\' x:xmptk=\\'Adobe XMP Core 4.2.2-c063 53.352624, 2008/07/30-18:12:18 \\'>\\r\\n\\t<rdf:RDF xmlns:rdf=\\'http://www.w3.org/1999/02/22-rdf-syntax-ns#\\'>\\r\\n\\t\\t<rdf:Description rdf:about=\\\\' xmlns:xmp=\\'http://ns.adobe.com/xap/1.0/\\' xmlns:dc=\\'http://purl.org/dc/elements/1.1/\\' xmlns:photoshop=\\'http://ns.adobe.com/photoshop/1.0/\\' xmlns:xmpMM=\\'http://ns.adobe.com/xap/1.0/mm/\\' xmlns:stEvt=\\'http://ns.adobe.com/xap/1.0/sType/ResourceEvent#\\' xmlns:tiff=\\'http://ns.adobe.com/tiff/1.0/\\' xmlns:exif=\\'http://ns.adobe.com/exif/1.0/\\' xmp:CreatorTool=\\'Adobe Photoshop CS4 Windows\\' xmp:CreateDate=\\'2011-03-09T17:09:27+01:00\\' xmp:ModifyDate=\\'2011-03-09T17:10:23+01:00\\' xmp:MetadataDate=\\'2011-03-09T17:10:23+01:00\\' dc:format=\\'image/jpeg\\' photoshop:ColorMode=\\'3\\' xmpMM:InstanceID=\\'xmp.iid:C525B6FA3E4AE01195AFE2861A054AE9\\' xmpMM:DocumentID=\\'xmp.did:C525B6FA3E4AE01195AFE2861A054AE9\\' xmpMM:OriginalDocumentID=\\'xmp.did:C525B6FA3E4AE01195AFE2861A054AE9\\' tiff:Orientation=\\'1\\' tiff:XResolution=\\'3000000/10000\\' tiff:YResolution=\\'3000000/10000\\' tiff:ResolutionUnit=\\'2\\' tiff:NativeDigest=\\'256,257,258,259,262,274,277,284,530,531,282,283,296,301,318,319,529,532,306,270,271,272,305,315,33432;DE7B977DF6EFF346F11FF6BE697281C5\\' exif:PixelXDimension=\\'200\\' exif:PixelYDimension=\\'45\\' exif:ColorSpace=\\'65535\\' exif:NativeDigest=\\'36864,40960,40961,37121,37122,40962,40963,37510,40964,36867,36868,33434,33437,34850,34852,34855,34856,37377,37378,37379,37380,37381,37382,37383,37384,37385,37386,37396,41483,41484,41486,41487,41488,41492,41493,41495,41728,41729,41730,41985,41986,41987,41988,41989,41990,41991,41992,41993,41994,41995,41996,42016,0,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,20,22,23,24,25,26,27,28,30;0E127CF2EACEDA719CA574253BD56212\\'>\\r\\n\\t\\t\\t<xmpMM:History>\\r\\n\\t\\t\\t\\t<rdf:Seq>\\r\\n\\t\\t\\t\\t\\t<rdf:li stEvt:action=\\'created\\' stEvt:instanceID=\\'xmp.iid:C525B6FA3E4AE01195AFE2861A054AE9\\' stEvt:when=\\'2011-03-09T17:10:23+01:00\\' stEvt:softwareAgent=\\'Adobe Photoshop CS4 Windows\\'/>\\r\\n\\t\\t\\t\\t</rdf:Seq>\\r\\n\\t\\t\\t</xmpMM:History>\\r\\n\\t\\t</rdf:Description>\\r\\n\\t</rdf:RDF>\\r\\n</x:xmpmeta>\\r\\n \\n o\\u000bP????\\u0006??T??/w?\\u0000\\u0007[???????', 'MailTechnician': 'email#email.it', 'WbsTechnician': 'xyz', 'NumberPincerTechnician': '' }
with the following code
using (StreamReader file = File.OpenText(#"path\\file.json"))
{
JsonSerializer serializer = new JsonSerializer();
tbl_tech tech = (tbl_tech)serializer.Deserialize(file, typeof(tbl_tech));
}
this is the object that i use
public partial class tbl_tech
{
public tbl_tech()
{
this.tbl_odl = new HashSet<tbl_odl>();
this.tbl_pdr = new HashSet<tbl_pdr>();
}
public long IdTechnician { get; set; }
public string DescTechnician { get; set; }
public string LoginTechnician { get; set; }
public string TelephoneTechnician { get; set; }
public string SignatureTechnician { get; set; }
public string MailTechnician { get; set; }
public string WbsTechnician { get; set; }
public string NumberPincerTechnician { get; set; }
public Nullable<long> TypeTechnician { get; set; }
public virtual ICollection<tbl_odl> tbl_odl { get; set; }
public virtual ICollection<tbl_pdr> tbl_pdr { get; set; }
}
and this is the error generate from visual studio.
{"Could not cast or convert from System.String to eStartService.tbl_tech."}
I do not understand is that the fields of the object are perfectly identical to those of my object , so where wrong assignment given to json ? I am that something wrong ? thanks
That json is an array of objects, not an object. See the [ and ] at the beginning/end?
tbl_tech[] tech = (tbl_tech[])serializer.Deserialize(sr, typeof(tbl_tech[]));
If I modify the code like this:
public partial class tbl_tech
{
public tbl_tech()
{
//this.tbl_odl = new HashSet<tbl_odl>();
//this.tbl_pdr = new HashSet<tbl_pdr>();
}
public long IdTechnician { get; set; }
public string DescTechnician { get; set; }
public string LoginTechnician { get; set; }
public string TelephoneTechnician { get; set; }
public string SignatureTechnician { get; set; }
public string MailTechnician { get; set; }
public string WbsTechnician { get; set; }
public string NumberPincerTechnician { get; set; }
public Nullable<long> TypeTechnician { get; set; }
//public virtual ICollection<tbl_odl> tbl_odl { get; set; }
//public virtual ICollection<tbl_pdr> tbl_pdr { get; set; }
}
The deserialization works fine.Becase now your json and your Type are similar.
Try using JSon2csharp when creating your JSon representation.
I removed the "'" from the numbers too:
[{'IdTechnician': 4, 'DescTechnician': 'Surname Name', 'LoginTechnician': 'name', 'TypeTechnician': 1, 'TelephoneTechnician': '+123456789', 'SignatureTechnician': 'signaturepath', 'MailTechnician': 'email#email.com', 'WbsTechnician': 'XYZ', 'NumberPincerTechnician': 1}]
Which gives the following model:
public class RootObject
{
public int IdTechnician { get; set; }
public string DescTechnician { get; set; }
public string LoginTechnician { get; set; }
public int TypeTechnician { get; set; }
public string TelephoneTechnician { get; set; }
public string SignatureTechnician { get; set; }
public string MailTechnician { get; set; }
public string WbsTechnician { get; set; }
public int NumberPincerTechnician { get; set; }
}
You can deserialize it in the following way:
IList<RootObject> result = new List<RootObject>();
using (StreamReader file = File.OpenText(#"path\\file.json"))
{
result = JsonConvert.DeserializeObject<List<RootObject>>(file.ReadToEnd());
}
So I have been able to get JSON objects for a few things, however this object is quite a bit more complex.
I'm trying to get comments from Reddit.
Here is the method I use:
public async Task<List<string>> GetComments(string currentSubreddit, string topicID)
{
string commentUrl = "http://www.reddit.com/r/" + currentSubreddit + "/comments/" + topicID + "/.json";
List<Comments> commentList = new List<Comments>();
string jsonText = await wc.GetJsonText(commentUrl);
Comments.RootObject deserializeObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Comments.RootObject>(jsonText);
List<string> commentListTest = new List<string>();
//List<string> commentListTest = deserializeObject.data.children[0].data.children;
return commentListTest;
}
This is the GetJsonText method:
public async Task<string> GetJsonText(string url)
{
var request = WebRequest.Create(url);
string text;
request.ContentType = "application/json; charset=utf-8";
var response = (HttpWebResponse)await request.GetResponseAsync();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
return text;
}
And here is a link to the Object: http://pastebin.com/WQ8XXGNA
And a link to the jsonText: http://pastebin.com/7Kh6cA9a
The error returned says this:
An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in mscorlib.dll but was not handled in user code
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'JuicyReddit.Comments+RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
I'd appreciate if anybody could help me with figuring out whats wrong with this.
Thanks
There are a few problems with your code actually
public async Task<List<string>> GetComments(string currentSubreddit, string topicID)
You don't need to return a list of string here, u need to return a full object
First rename RootObject in the model to an appropriate name such as "CommentsObject"
So set up your class like so and name it CommentsObject.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YOURNAMESPACE.Comments
{
public class MediaEmbed
{
}
public class SecureMediaEmbed
{
}
public class Data4
{
public int count { get; set; }
public string parent_id { get; set; }
public List<string> children { get; set; }
public string name { get; set; }
public string id { get; set; }
public string subreddit_id { get; set; }
public object banned_by { get; set; }
public string subreddit { get; set; }
public object likes { get; set; }
public object replies { get; set; }
public bool? saved { get; set; }
public int? gilded { get; set; }
public string author { get; set; }
public object approved_by { get; set; }
public string body { get; set; }
public object edited { get; set; }
public object author_flair_css_class { get; set; }
public int? downs { get; set; }
public string body_html { get; set; }
public string link_id { get; set; }
public bool? score_hidden { get; set; }
public double? created { get; set; }
public object author_flair_text { get; set; }
public double? created_utc { get; set; }
public object distinguished { get; set; }
public object num_reports { get; set; }
public int? ups { get; set; }
}
public class Child2
{
public string kind { get; set; }
public Data4 data { get; set; }
}
public class Data3
{
public string modhash { get; set; }
public List<Child2> children { get; set; }
public object after { get; set; }
public object before { get; set; }
}
public class Replies
{
public string kind { get; set; }
public Data3 data { get; set; }
}
public class Data2
{
public string domain { get; set; }
public object banned_by { get; set; }
public MediaEmbed media_embed { get; set; }
public string subreddit { get; set; }
public object selftext_html { get; set; }
public string selftext { get; set; }
public object likes { get; set; }
public object secure_media { get; set; }
public object link_flair_text { get; set; }
public string id { get; set; }
public SecureMediaEmbed secure_media_embed { get; set; }
public bool clicked { get; set; }
public bool stickied { get; set; }
public string author { get; set; }
public object media { get; set; }
public int score { get; set; }
public object approved_by { get; set; }
public bool over_18 { get; set; }
public bool hidden { get; set; }
public string thumbnail { get; set; }
public string subreddit_id { get; set; }
public object edited { get; set; }
public object link_flair_css_class { get; set; }
public object author_flair_css_class { get; set; }
public int downs { get; set; }
public bool saved { get; set; }
public bool is_self { get; set; }
public string permalink { get; set; }
public string name { get; set; }
public double created { get; set; }
public string url { get; set; }
public object author_flair_text { get; set; }
public string title { get; set; }
public double created_utc { get; set; }
public int ups { get; set; }
public int num_comments { get; set; }
public bool visited { get; set; }
public object num_reports { get; set; }
public object distinguished { get; set; }
public Replies replies { get; set; }
public int? gilded { get; set; }
public string parent_id { get; set; }
public string body { get; set; }
public string body_html { get; set; }
public string link_id { get; set; }
public bool? score_hidden { get; set; }
public int? count { get; set; }
public List<string> children { get; set; }
}
public class Child
{
public string kind { get; set; }
public Data2 data { get; set; }
}
public class Data
{
public string modhash { get; set; }
public List<Child> children { get; set; }
public object after { get; set; }
public object before { get; set; }
}
public class CommentsObject
{
public string kind { get; set; }
public Data data { get; set; }
}
}
Make your namespace correct!
Then handle the request and deserialise into a list of commentobjects: (u can use the webclient instead of httpclient if you want, this is just an example)
private HttpClient client;
public async Task<List<CommentsObject>> GetComments()
{
client = new HttpClient();
var response = await client.GetAsync("http://www.reddit.com/r/AskReddit/comments/1ut6xc.json");
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
List<CommentsObject> comments = await JsonConvert.DeserializeObjectAsync<List<CommentsObject>>(json);
return comments;
}
else
{
throw new Exception("Errorhandling message");
}
}
It's not ideal (and not completely an answer but more of a work around) but I created models that mock the reddit response json to make deserialization super easy. I use JsonProperty attributes on my model properties to pretty up the models a bit.
Here are the models
And since my models directly mock the json I can just use json.net's generic deserialize method.