Deserializing JSON objects, keep getting errors (C#) - c#

I am trying to deserialize my json code. The json code is in a string, and the json code looks like this (so I'm assuming it's json objects)
{
"post_id":13,
"thread_id":9,
"user_id":1,
"username":"Username",
"post_date":1496439611,
"message":"testzilla - 2133746943A9",
"ip_id":698,
"message_state":"visible",
"attach_count":0,
"position":0,
"likes":0,
"like_users":"a:0:{}",
"warning_id":0,
"warning_message":"",
"last_edit_date":1496476199,
"last_edit_user_id":0,
"edit_count":9,
"node_id":34,
"title":"Test",
"tags":"a:0:{}",
"node_title":"test node",
"node_name":null,
"message_html":"testzilla - 2133746943A9",
"absolute_url":"url"
}
How would I put the "message" container inside a string? So that the string would contain "testzilla - 2133746943A9" without the quotation marks. I am using JSON.Net
The name of the string that contains this json code is "MACs". Thanks in advance. PS: I am a new coder.

there is a missing "{" at the beginning of your json file, try adding it

You need to create your c# class to deserialize your json string. As per your json structure i have created your class given below
public class MyClass
{
public int post_id { get; set; }
public int thread_id { get; set; }
public int user_id { get; set; }
public string username { get; set; }
public int post_date { get; set; }
public string message { get; set; }
public int ip_id { get; set; }
public string message_state { get; set; }
public int attach_count { get; set; }
public int position { get; set; }
public int likes { get; set; }
public string like_users { get; set; }
public int warning_id { get; set; }
public string warning_message { get; set; }
public int last_edit_date { get; set; }
public int last_edit_user_id { get; set; }
public int edit_count { get; set; }
public int node_id { get; set; }
public string title { get; set; }
public string tags { get; set; }
public string node_title { get; set; }
public object node_name { get; set; }
public string message_html { get; set; }
public string absolute_url { get; set; }
}
No need to use library JSON.Net. You can do this by simply using System.Web.Script.Serialization to deserialize the json string.
Note : System.Web.Script.Serialization is available inside System.Web.Extensions namespace.
Below is the complete code. I kept your json data inside a file named as "test2.json" and consuming it from that file.
using System;
using System.Web.Script.Serialization;
using System.IO;
namespace DesrializeJson1ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var jsonFile = "test2.json";
string jsonstring = File.ReadAllText(jsonFile);
var serializer = new JavaScriptSerializer();
MyClass aClass = serializer.Deserialize<MyClass>(jsonstring);
Console.WriteLine("--------------------------");
Console.WriteLine("message :" + aClass.message);
Console.WriteLine("message_state :" + aClass.message_state);
Console.WriteLine("warning_message :" + aClass.warning_message);
Console.WriteLine("message_html :" + aClass.message_html);
Console.WriteLine("--------------------------");
Console.Read();
}
}
public class MyClass
{
public int post_id { get; set; }
public int thread_id { get; set; }
public int user_id { get; set; }
public string username { get; set; }
public int post_date { get; set; }
public string message { get; set; }
public int ip_id { get; set; }
public string message_state { get; set; }
public int attach_count { get; set; }
public int position { get; set; }
public int likes { get; set; }
public string like_users { get; set; }
public int warning_id { get; set; }
public string warning_message { get; set; }
public int last_edit_date { get; set; }
public int last_edit_user_id { get; set; }
public int edit_count { get; set; }
public int node_id { get; set; }
public string title { get; set; }
public string tags { get; set; }
public string node_title { get; set; }
public object node_name { get; set; }
public string message_html { get; set; }
public string absolute_url { get; set; }
}
}
OUTPUT

You can use regex to get the value you want.
string yourJsonString = #"{ ""post_id"":13, ""thread_id"":9, ""user_id"":1, ""username"":""Username"", ""post_date"":1496439611, ""message"":""testzilla - 2133746943A9"", ""ip_id"":698, ""message_state"":""visible"", ""attach_count"":0, ""position"":0, ""likes"":0, ""like_users"":""a:0:{}"", ""warning_id"":0, ""warning_message"":"""", ""last_edit_date"":1496476199, ""last_edit_user_id"":0, ""edit_count"":9, ""node_id"":34, ""title"":""Test"", ""tags"":""a:0:{}"", ""node_title"":""test node"", ""node_name"":null, ""message_html"":""testzilla - 2133746943A9"", ""absolute_url"":""url""}";
string value = System.Text.RegularExpressions.Regex.Match(yourJsonString,#"""message"":(.+?),").Groups[1].Value.Replace(#"""","");
MessageBox.Show(value);

You can also use dynamic type for deserialization. You will no need to write object for deserialization:
var str = "{\r\n \"post_id\":13,\r\n \"thread_id\":9,\r\n \"user_id\":1,\r\n \"username\":\"Username\",\r\n \"post_date\":1496439611,\r\n \"message\":\"testzilla - 2133746943A9\",\r\n \"ip_id\":698,\r\n \"message_state\":\"visible\",\r\n \"attach_count\":0,\r\n \"position\":0,\r\n \"likes\":0,\r\n \"like_users\":\"a:0:{}\",\r\n \"warning_id\":0,\r\n \"warning_message\":\"\",\r\n \"last_edit_date\":1496476199,\r\n \"last_edit_user_id\":0,\r\n \"edit_count\":9,\r\n \"node_id\":34,\r\n \"title\":\"Test\",\r\n \"tags\":\"a:0:{}\",\r\n \"node_title\":\"test node\",\r\n \"node_name\":null,\r\n \"message_html\":\"testzilla - 2133746943A9\",\r\n \"absolute_url\":\"url\"\r\n}";
dynamic obj = JsonConvert.DeserializeObject(str);
var postId = obj.post_id;
Console.WriteLine("postId:" + postId);
output:
postId:13

Related

C# Getting individual data from JSON text downloaded from a site

I've been searching around for a long while for this, I haven't found any solutions to my issue which is:
I've been trying get a json data individually from a whole source seen here:
{"TargetId":0,"ProductType":null,"AssetId":1239281845,"ProductId":0,"Name":"❤️🍀𝐒𝐀𝐋𝐄❗️🍀❤️ Red&Black Flannel + Backpack","Description":"Shirt Image","AssetTypeId":1,"Creator":{"Id":124026176,"Name":"TheDestroyerPeter","CreatorType":"User","CreatorTargetId":124026176},"IconImageAssetId":0,"Created":"2017-12-12T19:48:24.693Z","Updated":"2017-12-12T19:48:24.693Z","PriceInRobux":null,"PriceInTickets":null,"Sales":0,"IsNew":false,"IsForSale":false,"IsPublicDomain":false,"IsLimited":false,"IsLimitedUnique":false,"Remaining":null,"MinimumMembershipLevel":0,"ContentRatingTypeId":0}
now what I've been trying to do with it is get the Product Name using C# and the product name is "❤️🍀𝐒𝐀𝐋𝐄❗️🍀❤️ Red&Black Flannel + Backpack", my issue is that I haven't found a way to extract the data, and when I have I haven't been able to get the right data, because instead if gives me "TheDestroyerPeter"
I've written up code, and deleted it, it was really sloppy and it would take awhile to rewrite, I appreciate any solutions
-whoever I am
You can use JavaScriptSerializer class, which is part of the System.Web.Script namespace.
For example :
var jsonString = #"{""name"":""John Doe"",""age"":20}";
var JSONObj = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(jsonString );
and then JSONObj["name"]; gives you "John Doe"
in this case you can use it :
public class Creator
{
public int Id { get; set; }
public string Name { get; set; }
public string CreatorType { get; set; }
public int CreatorTargetId { get; set; }
}
public class RootObject
{
public int TargetId { get; set; }
public object ProductType { get; set; }
public int AssetId { get; set; }
public int ProductId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int AssetTypeId { get; set; }
public Creator Creator { get; set; }
public int IconImageAssetId { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public object PriceInRobux { get; set; }
public object PriceInTickets { get; set; }
public int Sales { get; set; }
public bool IsNew { get; set; }
public bool IsForSale { get; set; }
public bool IsPublicDomain { get; set; }
public bool IsLimited { get; set; }
public bool IsLimitedUnique { get; set; }
public object Remaining { get; set; }
public int MinimumMembershipLevel { get; set; }
public int ContentRatingTypeId { get; set; }
}
use Newtonsoft.Json
var jsonString = #"{""TargetId"":0,""ProductType"":null,""AssetId"":1239281845,""ProductId"":0,""Name"":""❤️🍀𝐒𝐀𝐋𝐄❗️🍀❤️ Red&Black Flannel + Backpack"",""Description"":""Shirt Image"",""AssetTypeId"":1,""Creator"":{""Id"":124026176,""Name"":""TheDestroyerPeter"",""CreatorType"":""User"",""CreatorTargetId"":124026176},""IconImageAssetId"":0,""Created"":""2017-12-12T19:48:24.693Z"",""Updated"":""2017-12-12T19:48:24.693Z"",""PriceInRobux"":null,""PriceInTickets"":null,""Sales"":0,""IsNew"":false,""IsForSale"":false,""IsPublicDomain"":false,""IsLimited"":false,""IsLimitedUnique"":false,""Remaining"":null,""MinimumMembershipLevel"":0,""ContentRatingTypeId"":0}";
var obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
Console.WriteLine(obj.Creator.Name); //"TheDestroyerPeter"

Json string deserialization C#

I have this json
and i want to deserialize it so I can get each object's value for example:
"icon_url": "-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXH5ApeO4YmlhxYQknCRvCo04DEVlxkKgpouLWzKjhzw8zFdC5K092kl5SClMj3PLXFhGpC_Pp8j-3I4IG7i1Hn_UI-Nmj3ItDGe1BoN1mCr1G4xL_vhMS8tcmcn3JhuihwsHvbzQv3309k3tBw8A",
The problem is I can make the class(es) that I need so I can deserialize the json because the json string has nested objects.
I used json2csharp to help me generate classes. After some merging and cleaning up, this is what I got:
public class InventoryItem
{
public string id { get; set; }
public string classid { get; set; }
public string instanceid { get; set; }
public string amount { get; set; }
public int pos { get; set; }
}
public class AppData
{
public string def_index { get; set; }
public int? is_itemset_name { get; set; }
public int? limited { get; set; }
}
public class Description
{
public string type { get; set; }
public string value { get; set; }
public string color { get; set; }
public AppData app_data { get; set; }
}
public class Action
{
public string name { get; set; }
public string link { get; set; }
}
public class Tag
{
public string internal_name { get; set; }
public string name { get; set; }
public string category { get; set; }
public string category_name { get; set; }
public string color { get; set; }
}
public class RgDescription
{
public string appid { get; set; }
public string classid { get; set; }
public string instanceid { get; set; }
public string icon_url { get; set; }
public string icon_url_large { get; set; }
public string icon_drag_url { get; set; }
public string name { get; set; }
public string market_hash_name { get; set; }
public string market_name { get; set; }
public string name_color { get; set; }
public string background_color { get; set; }
public string type { get; set; }
public int tradable { get; set; }
public int marketable { get; set; }
public int commodity { get; set; }
public string market_tradable_restriction { get; set; }
public List<Description> descriptions { get; set; }
public List<Action> actions { get; set; }
public List<Action> market_actions { get; set; }
public List<Tag> tags { get; set; }
}
public class RootObject
{
public bool success { get; set; }
public IDictionary<string, InventoryItem> rgInventory { get; set; }
public List<object> rgCurrency { get; set; }
public IDictionary<string, RgDescription> rgDescriptions { get; set; }
public bool more { get; set; }
public bool more_start { get; set; }
}
These appear to work correctly, you can deserialize and serialize with code like this:
var obj = JsonConvert.DeserializeObject<RootObject>(oldString);
Console.WriteLine(obj.rgDescriptions["310776560_302028390"].icon_url); // e.g.
var newString = JsonConvert.SerializeObject(obj,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
// null value handling is optional, the above makes it a little more like the source string

deserializing json object in c # with json.net , error reading/cast

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());
}

I need to parse json result in windows phone app c#

Im new in developing windows phone app --> i have problem so i have tried all the possible solutions but with no vain:-
the application tried to take result from webservice the result come back like this:-
{"d":"{\"sessionid\":null,\"VersionInfo\":{\"Rel\":0,\"Ver\":0,\"Patch\":0,\"ForceUpdate\":0,\"UpdateType\":0,\"Globals\":{\"MultiSessionsAllowed\":true,\"CommCalcType\":1,\"PriceChangedTimer\":20,\"ValidLotsLocation\":2,\"CustumizeTradeMsg\":true,\"FirstWhiteLabeledOffice\":null,\"DealerTreePriv\":0,\"ClientConnectTimer\":200,\"ClientTimeoutTimer\":500,\"DefaultLots\":0.01,\"WebSecurityID\":\"agagag\",\"ServerGMT\":3}},\"SystemLockInfo\":{\"MinutesRemaining\":0,\"HoursRemaining\":0,\"DaysRemaining\":0,\"Maintanance\":0,\"WillBeLocked\":1},\"FirstWhiteLabel\":\"HS Dev\",\"WLID\":\"3\",\"CheckWhiteLabel\":true,\"Password\":\"1234\",\"Username\":\"obeidat\",\"LastTickTime\":\"\/Date(1396613678728)\/\",\"SelectedAccount\":12345791,\"Name\":0,\"CompanyName\":\"HS Dev\",\"UserId\":579,\"DemoClient\":\"0\",\"FName\":\"obeidat\",\"SName\":null,\"TName\":null,\"LName\":null,\"Sms\":null,\"isReadOnly\":\"0\",\"SchSms\":\"0\",\"AlertSms\":\"0\",\"Temp\":null,\"GMTOffset\":\"5\",\"SvrGMT\":\"3\",\"ClientType\":null,\"EnableNews\":\"0\",\"PublicSlideNews\":\"\",\"PrivateSlideNews\":\"Thanks for using our platform##We will inform you here with any private news\",\"DealerTreePriv\":1}"}
so i have tried to parse it but it give me errors after that i have tried locally removed the {"d":" and \"LastTickTime\":\"\/Date(1396613678728)\/\" from the string with no connection to webservice and its working fine i use this code :-
// Constructor
public MainPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
//Json classes
public class OuterRootObject
{
public string d { get; set; }
}
public class Globals
{
public bool MultiSessionsAllowed { get; set; }
public int CommCalcType { get; set; }
public int PriceChangedTimer { get; set; }
public int ValidLotsLocation { get; set; }
public bool CustumizeTradeMsg { get; set; }
public object FirstWhiteLabeledOffice { get; set; }
public int DealerTreePriv { get; set; }
public int ClientConnectTimer { get; set; }
public int ClientTimeoutTimer { get; set; }
public double DefaultLots { get; set; }
public string WebSecurityID { get; set; }
public int ServerGMT { get; set; }
}
public class VersionInfo
{
public int Rel { get; set; }
public int Ver { get; set; }
public int Patch { get; set; }
public int ForceUpdate { get; set; }
public int UpdateType { get; set; }
public Globals Globals { get; set; }
}
public class SystemLockInfo
{
public int MinutesRemaining { get; set; }
public int HoursRemaining { get; set; }
public int DaysRemaining { get; set; }
public int Maintanance { get; set; }
public int WillBeLocked { get; set; }
}
public class RootObject
{
public string sessionid { get; set; }
public VersionInfo VersionInfo { get; set; }
public SystemLockInfo SystemLockInfo { get; set; }
public string FirstWhiteLabel { get; set; }
public string WLID { get; set; }
public bool CheckWhiteLabel { get; set; }
public string Password { get; set; }
public string Username { get; set; }
public DateTime LastTickTime { get; set; }
public int SelectedAccount { get; set; }
public int Name { get; set; }
public object ServicePath { get; set; }
public string GWSessionID { get; set; }
public string IP { get; set; }
public string SessionDateStart { get; set; }
public string CompanyName { get; set; }
public string UserId { get; set; }
public string DemoClient { get; set; }
public string FName { get; set; }
public string SName { get; set; }
public string TName { get; set; }
public string LName { get; set; }
public object Sms { get; set; }
public string isReadOnly { get; set; }
public string SchSms { get; set; }
public string AlertSms { get; set; }
public object Temp { get; set; }
public string GMTOffset { get; set; }
public string SvrGMT { get; set; }
public object ClientType { get; set; }
public string EnableNews { get; set; }
public string PublicSlideNews { get; set; }
public string PrivateSlideNews { get; set; }
public int DealerTreePriv { get; set; }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var jsonString =
"{\"sessionid\":null,\"VersionInfo\":{\"Rel\":0,\"Ver\":0,\"Patch\":0,\"ForceUpdate\":0,\"UpdateType\":0,\"Globals\":{\"MultiSessionsAllowed\":true,\"CommCalcType\":1,\"PriceChangedTimer\":20,\"ValidLotsLocation\":2,\"CustumizeTradeMsg\":true,\"FirstWhiteLabeledOffice\":null,\"DealerTreePriv\":0,\"ClientConnectTimer\":200,\"ClientTimeoutTimer\":500,\"DefaultLots\":0.01,\"WebSecurityID\":\"agagag\",\"ServerGMT\":3}},\"SystemLockInfo\":{\"MinutesRemaining\":0,\"HoursRemaining\":0,\"DaysRemaining\":0,\"Maintanance\":0,\"WillBeLocked\":1},\"FirstWhiteLabel\":\"HS Dev\",\"WLID\":\"3\",\"CheckWhiteLabel\":true,\"Password\":\"1234\",\"Username\":\"obeidat\",\"SelectedAccount\":12345791,\"Name\":0,\"CompanyName\":\"HS Dev\",\"UserId\":-579,\"DemoClient\":\"0\",\"FName\":\"obeidat\",\"SName\":null,\"TName\":null,\"LName\":null,\"Sms\":null,\"isReadOnly\":\"0\",\"SchSms\":\"0\",\"AlertSms\":\"0\",\"Temp\":null,\"GMTOffset\":\"5\",\"SvrGMT\":\"3\",\"ClientType\":null,\"EnableNews\":\"0\",\"PublicSlideNews\":\"\",\"PrivateSlideNews\":\"Thanks for using our platform##We will inform you here with any private news\",\"DealerTreePriv\":1}";
RootObject jsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
MessageBox.Show("hello " + jsonObject.Username + "" + jsonObject.UserId);
int val1 = Convert.ToInt16(jsonObject.UserId);
if (val1 > 0)
MessageBox.Show("You are logedin");
else
{
MessageBox.Show("Sorry Please login");
}
}
// // Create a new button and set the text value to the localized string from AppResources.
// ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
// appBarButton.Text = AppResources.AppBarButtonText;
// ApplicationBar.Buttons.Add(appBarButton);
// // Create a new menu item with the localized string from AppResources.
// ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
// ApplicationBar.MenuItems.Add(appBarMenuItem);
//}
}
}
Please help me to parse all of the json result (locally i mean without conection to web)and what is the code to do that.
thank you all
First, your JSON is all a string property of "D". You can't deserialize it to RootClass. Second, your JSON contains invalid escape characters in the LastTickTime area. I removed both of these, and the JSON parses without errors of any kind. I just used a console app to test it.
var s = "{\"sessionid\":null,\"VersionInfo\":{\"Rel\":0,\"Ver\":0,\"Patch\":0,\"ForceUpdate\":0,\"UpdateType\":0,\"Globals\":{\"MultiSessionsAllowed\":true,\"CommCalcType\":1,\"PriceChangedTimer\":20,\"ValidLotsLocation\":2,\"CustumizeTradeMsg\":true,\"FirstWhiteLabeledOffice\":null,\"DealerTreePriv\":0,\"ClientConnectTimer\":200,\"ClientTimeoutTimer\":500,\"DefaultLots\":0.01,\"WebSecurityID\":\"agagag\",\"ServerGMT\":3}},\"SystemLockInfo\":{\"MinutesRemaining\":0,\"HoursRemaining\":0,\"DaysRemaining\":0,\"Maintanance\":0,\"WillBeLocked\":1},\"FirstWhiteLabel\":\"HS Dev\",\"WLID\":\"3\",\"CheckWhiteLabel\":true,\"Password\":\"1234\",\"Username\":\"obeidat\",\"SelectedAccount\":12345791,\"Name\":0,\"CompanyName\":\"HS Dev\",\"UserId\":579,\"DemoClient\":\"0\",\"FName\":\"obeidat\",\"SName\":null,\"TName\":null,\"LName\":null,\"Sms\":null,\"isReadOnly\":\"0\",\"SchSms\":\"0\",\"AlertSms\":\"0\",\"Temp\":null,\"GMTOffset\":\"5\",\"SvrGMT\":\"3\",\"ClientType\":null,\"EnableNews\":\"0\",\"PublicSlideNews\":\"\",\"PrivateSlideNews\":\"Thanks for using our platform##We will inform you here with any private news\",\"DealerTreePriv\":1}";
var foo = JsonConvert.DeserializeObject<RootObject>(s);
Console.WriteLine(foo.UserId); // Writes 579
UPDATE
If you have no control over what you get back, do the following:
Take the response and get rid of the / combination. As such:
string webResponse = "your long web response from the server";
webResponse = webResponse.Replace(#"\/", "/");
// If dynamic isn't in the phone subset, you'll need a class here containing "d" as a string.
var jsonOuter = JsonConvert.DeserializeObject<dynamic>(webResponse);
var jsonInner = JsonConvert.DeserializeObject<RootObject>(jsonOuter.d);
if (jsonInner.UserId > .....)

C# JSON Object wont deserialize

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.

Categories