C# Turn JSON Into Data - c#

I'm using a program (Tiled) to create tilesets for a game, which spits out JSON files similar to this:
http://pastebin.com/t7UHzG7w
And i'm looking to turn it into data that I can use (a map of the "data", or an int[][] of the "data", as well as the width/height, all the other info is extraneous and I know already), and don't really know how to go about it.
How do I go from that JSON to data in a format I can deal with it?

You should use create a model class to represent the JSON data you have. Then you can use JavaScriptSerializer or Newsoft JOSN library to convert this data into object.
You should create model class for your data:
public class Layer
{
public List<int> data { get; set; }
public int height { get; set; }
public string name { get; set; }
public int opacity { get; set; }
public string type { get; set; }
public bool visible { get; set; }
public int width { get; set; }
public int x { get; set; }
public int y { get; set; }
}
public class Tileset
{
public int columns { get; set; }
public int firstgid { get; set; }
public string image { get; set; }
public int imageheight { get; set; }
public int imagewidth { get; set; }
public int margin { get; set; }
public string name { get; set; }
public int spacing { get; set; }
public int tilecount { get; set; }
public int tileheight { get; set; }
public int tilewidth { get; set; }
}
public class Data
{
public int height { get; set; }
public List<Layer> layers { get; set; }
public int nextobjectid { get; set; }
public string orientation { get; set; }
public string renderorder { get; set; }
public int tileheight { get; set; }
public List<Tileset> tilesets { get; set; }
public int tilewidth { get; set; }
public int version { get; set; }
public int width { get; set; }
}
Once this is done, you can use Newtonsoft.Json library to parse the string data into this object.
string text = "<Your Json data>";
var result = JsonConvert.DeserializeObject<Data>(text);
You can download Newtonsoft JSON library from here:
http://www.newtonsoft.com/json
Or use NPM as well:
Install-Package Newtonsoft.Json

Newtonsoft.Json is your friend.
https://www.nuget.org/packages/Newtonsoft.Json
http://www.newtonsoft.com/json/help/html/DeserializeObject.htm

You could use Newtonsoft.Json. Then after you declare your object / model, you could use it like
string Json = ""; // your Json string
var Result = JsonConvert.DeserializeObject<YourModel>(Json);
To get the Package, You can use the Nugget Function and type :
Install-Package Newtonsoft.Json

Related

i can't deserializing into JSON in C#. Is it something wrong with my classes?

I'm having trouble when tryin to Deserialize a string into JSON.
I'm making a call to a API. The anwer i get is stored i a variable(body)
Root Api = JsonConvert.DeserializeObject<Root>(body);
//var json = JsonConvert.DeserializeObject<Root>(body);
public partial class All
{
public int matchsPlayed { get; set; }
public int win { get; set; }
public int draw { get; set; }
public int lose { get; set; }
public int goalsFor { get; set; }
public int goalsAgainst { get; set; }
}
public partial class Home
{
public int matchsPlayed { get; set; }
public int win { get; set; }
public int draw { get; set; }
public int lose { get; set; }
public int goalsFor { get; set; }
public int goalsAgainst { get; set; }
}
public partial class Away
{
public int matchsPlayed { get; set; }
public int win { get; set; }
public int draw { get; set; }
public int lose { get; set; }
public int goalsFor { get; set; }
public int goalsAgainst { get; set; }
}
public partial class Standings // Offers
{
public int rank { get; set; }
public int team_id { get; set; }
public string teamName { get; set; }
public string logo { get; set; }
public string group { get; set; }
public string forme { get; set; }
public string status { get; set; }
public string description { get; set; }
public All all { get; set; }
public Home home { get; set; }
public Away away { get; set; }
public int goalsDiff { get; set; }
public int points { get; set; }
public string lastUpdate { get; set; }
}
public partial class Api // ProductListing
{
public int results { get; set; }
// public Standings[] Standings { get; set; }
public List<Standings> standings { get; set; }
}
public partial class Root // RootObject
{
public Api api { get; set; }
}
I get this error message: "
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'Standings' because the type requires a JSON object (e.g.
{"name":"value"}) to deserialize correctly. To fix this error either
change the JSON to a JSON object (e.g. {"name":"value"}) or change the
deserialized type to an array or a type that implements a collection
interface (e.g. ICollection, IList) like List that can be
deserialized from a JSON array. JsonArrayAttribute can also be added
to the type to force it to deserialize from a JSON array.
"
This is the whole json text i'm tryin to deserialize:
> {"api":{"results":1,"standings":[[{"rank":1,"team_id":45,"teamName":"Everton","logo":"https:\/\/media.api-sports.io\/football\/teams\/45.png","group":"Premier League","forme":"DWWWW","status":"same","description":"Promotion - Champions League (Group Stage)","all":{"matchsPlayed":5,"win":4,"draw":1,"lose":0,"goalsFor":14,"goalsAgainst":7},"home":{"matchsPlayed":3,"win":2,"draw":1,"lose":0,"goalsFor":11,"goalsAgainst":6},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":3,"goalsAgainst":1},"goalsDiff":7,"points":13,"lastUpdate":"2020-10-19"},{"rank":2,"team_id":66,"teamName":"Aston Villa","logo":"https:\/\/media.api-sports.io\/football\/teams\/66.png","group":"Premier League","forme":"WWWW","status":"same","description":"Promotion - Champions League (Group Stage)","all":{"matchsPlayed":4,"win":4,"draw":0,"lose":0,"goalsFor":12,"goalsAgainst":2},"home":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":8,"goalsAgainst":2},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":4,"goalsAgainst":0},"goalsDiff":10,"points":12,"lastUpdate":"2020-10-19"},{"rank":3,"team_id":40,"teamName":"Liverpool","logo":"https:\/\/media.api-sports.io\/football\/teams\/40.png","group":"Premier League","forme":"DLWWW","status":"same","description":"Promotion - Champions League (Group Stage)","all":{"matchsPlayed":5,"win":3,"draw":1,"lose":1,"goalsFor":13,"goalsAgainst":13},"home":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":7,"goalsAgainst":4},"away":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":6,"goalsAgainst":9},"goalsDiff":0,"points":10,"lastUpdate":"2020-10-19"},{"rank":4,"team_id":46,"teamName":"Leicester","logo":"https:\/\/media.api-sports.io\/football\/teams\/46.png","group":"Premier League","forme":"LLWWW","status":"same","description":"Promotion - Champions League (Group Stage)","all":{"matchsPlayed":5,"win":3,"draw":0,"lose":2,"goalsFor":12,"goalsAgainst":8},"home":{"matchsPlayed":3,"win":1,"draw":0,"lose":2,"goalsFor":4,"goalsAgainst":6},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":8,"goalsAgainst":2},"goalsDiff":4,"points":9,"lastUpdate":"2020-10-19"},{"rank":5,"team_id":42,"teamName":"Arsenal","logo":"https:\/\/media.api-sports.io\/football\/teams\/42.png","group":"Premier League","forme":"LWLWW","status":"same","description":"Promotion - Europa League (Group Stage)","all":{"matchsPlayed":5,"win":3,"draw":0,"lose":2,"goalsFor":8,"goalsAgainst":6},"home":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":4,"goalsAgainst":2},"away":{"matchsPlayed":3,"win":1,"draw":0,"lose":2,"goalsFor":4,"goalsAgainst":4},"goalsDiff":2,"points":9,"lastUpdate":"2020-10-19"},{"rank":6,"team_id":39,"teamName":"Wolves","logo":"https:\/\/media.api-sports.io\/football\/teams\/39.png","group":"Premier League","forme":"WWLLW","status":"same","description":null,"all":{"matchsPlayed":5,"win":3,"draw":0,"lose":2,"goalsFor":5,"goalsAgainst":7},"home":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":2,"goalsAgainst":3},"away":{"matchsPlayed":3,"win":2,"draw":0,"lose":1,"goalsFor":3,"goalsAgainst":4},"goalsDiff":-2,"points":9,"lastUpdate":"2020-10-19"},{"rank":7,"team_id":47,"teamName":"Tottenham","logo":"https:\/\/media.api-sports.io\/football\/teams\/47.png","group":"Premier League","forme":"DWDWL","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":2,"lose":1,"goalsFor":15,"goalsAgainst":8},"home":{"matchsPlayed":3,"win":0,"draw":2,"lose":1,"goalsFor":4,"goalsAgainst":5},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":11,"goalsAgainst":3},"goalsDiff":7,"points":8,"lastUpdate":"2020-10-19"},{"rank":8,"team_id":49,"teamName":"Chelsea","logo":"https:\/\/media.api-sports.io\/football\/teams\/49.png","group":"Premier League","forme":"DWDLW","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":2,"lose":1,"goalsFor":13,"goalsAgainst":9},"home":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":7,"goalsAgainst":5},"away":{"matchsPlayed":2,"win":1,"draw":1,"lose":0,"goalsFor":6,"goalsAgainst":4},"goalsDiff":4,"points":8,"lastUpdate":"2020-10-19"},{"rank":9,"team_id":48,"teamName":"West Ham","logo":"https:\/\/media.api-sports.io\/football\/teams\/48.png","group":"Premier League","forme":"DWWLL","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":11,"goalsAgainst":7},"home":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":4,"goalsAgainst":2},"away":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":7,"goalsAgainst":5},"goalsDiff":4,"points":7,"lastUpdate":"2020-10-19"},{"rank":10,"team_id":63,"teamName":"Leeds","logo":"https:\/\/media.api-sports.io\/football\/teams\/63.png","group":"Premier League","forme":"LDWWL","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":9,"goalsAgainst":9},"home":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":5,"goalsAgainst":5},"away":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":4,"goalsAgainst":4},"goalsDiff":0,"points":7,"lastUpdate":"2020-10-19"},{"rank":11,"team_id":50,"teamName":"Manchester City","logo":"https:\/\/media.api-sports.io\/football\/teams\/50.png","group":"Premier League","forme":"WDLW","status":"same","description":null,"all":{"matchsPlayed":4,"win":2,"draw":1,"lose":1,"goalsFor":7,"goalsAgainst":7},"home":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":3,"goalsAgainst":5},"away":{"matchsPlayed":2,"win":1,"draw":1,"lose":0,"goalsFor":4,"goalsAgainst":2},"goalsDiff":0,"points":7,"lastUpdate":"2020-10-19"},{"rank":12,"team_id":41,"teamName":"Southampton","logo":"https:\/\/media.api-sports.io\/football\/teams\/41.png","group":"Premier League","forme":"DWWLL","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":8,"goalsAgainst":9},"home":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":4,"goalsAgainst":5},"away":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":4,"goalsAgainst":4},"goalsDiff":-1,"points":7,"lastUpdate":"2020-10-19"},{"rank":13,"team_id":34,"teamName":"Newcastle","logo":"https:\/\/media.api-sports.io\/football\/teams\/34.png","group":"Premier League","forme":"LWDLW","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":7,"goalsAgainst":9},"home":{"matchsPlayed":3,"win":1,"draw":0,"lose":2,"goalsFor":4,"goalsAgainst":8},"away":{"matchsPlayed":2,"win":1,"draw":1,"lose":0,"goalsFor":3,"goalsAgainst":1},"goalsDiff":-2,"points":7,"lastUpdate":"2020-10-19"},{"rank":14,"team_id":52,"teamName":"Crystal Palace","logo":"https:\/\/media.api-sports.io\/football\/teams\/52.png","group":"Premier League","forme":"DLLWW","status":"same","description":null,"all":{"matchsPlayed":5,"win":2,"draw":1,"lose":2,"goalsFor":6,"goalsAgainst":8},"home":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":3,"goalsAgainst":3},"away":{"matchsPlayed":2,"win":1,"draw":0,"lose":1,"goalsFor":3,"goalsAgainst":5},"goalsDiff":-2,"points":7,"lastUpdate":"2020-10-19"},{"rank":15,"team_id":33,"teamName":"Manchester United","logo":"https:\/\/media.api-sports.io\/football\/teams\/33.png","group":"Premier League","forme":"WLWL","status":"same","description":null,"all":{"matchsPlayed":4,"win":2,"draw":0,"lose":2,"goalsFor":9,"goalsAgainst":12},"home":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":2,"goalsAgainst":9},"away":{"matchsPlayed":2,"win":2,"draw":0,"lose":0,"goalsFor":7,"goalsAgainst":3},"goalsDiff":-3,"points":6,"lastUpdate":"2020-10-19"},{"rank":16,"team_id":51,"teamName":"Brighton","logo":"https:\/\/media.api-sports.io\/football\/teams\/51.png","group":"Premier League","forme":"DLLWL","status":"same","description":null,"all":{"matchsPlayed":5,"win":1,"draw":1,"lose":3,"goalsFor":9,"goalsAgainst":11},"home":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":3,"goalsAgainst":6},"away":{"matchsPlayed":3,"win":1,"draw":1,"lose":1,"goalsFor":6,"goalsAgainst":5},"goalsDiff":-2,"points":4,"lastUpdate":"2020-10-19"},{"rank":17,"team_id":60,"teamName":"West Brom","logo":"https:\/\/media.api-sports.io\/football\/teams\/60.png","group":"Premier League","forme":"DLDLL","status":"same","description":null,"all":{"matchsPlayed":5,"win":0,"draw":2,"lose":3,"goalsFor":5,"goalsAgainst":13},"home":{"matchsPlayed":3,"win":0,"draw":2,"lose":1,"goalsFor":3,"goalsAgainst":6},"away":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":2,"goalsAgainst":7},"goalsDiff":-8,"points":2,"lastUpdate":"2020-10-19"},{"rank":18,"team_id":44,"teamName":"Burnley","logo":"https:\/\/media.api-sports.io\/football\/teams\/44.png","group":"Premier League","forme":"DLLL","status":"same","description":"Relegation - Championship","all":{"matchsPlayed":4,"win":0,"draw":1,"lose":3,"goalsFor":3,"goalsAgainst":8},"home":{"matchsPlayed":1,"win":0,"draw":0,"lose":1,"goalsFor":0,"goalsAgainst":1},"away":{"matchsPlayed":3,"win":0,"draw":1,"lose":2,"goalsFor":3,"goalsAgainst":7},"goalsDiff":-5,"points":1,"lastUpdate":"2020-10-19"},{"rank":19,"team_id":62,"teamName":"Sheffield Utd","logo":"https:\/\/media.api-sports.io\/football\/teams\/62.png","group":"Premier League","forme":"DLLLL","status":"same","description":"Relegation - Championship","all":{"matchsPlayed":5,"win":0,"draw":1,"lose":4,"goalsFor":2,"goalsAgainst":7},"home":{"matchsPlayed":3,"win":0,"draw":1,"lose":2,"goalsFor":1,"goalsAgainst":4},"away":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":1,"goalsAgainst":3},"goalsDiff":-5,"points":1,"lastUpdate":"2020-10-19"},{"rank":20,"team_id":36,"teamName":"Fulham","logo":"https:\/\/media.api-sports.io\/football\/teams\/36.png","group":"Premier League","forme":"DLLLL","status":"same","description":"Relegation - Championship","all":{"matchsPlayed":5,"win":0,"draw":1,"lose":4,"goalsFor":4,"goalsAgainst":12},"home":{"matchsPlayed":2,"win":0,"draw":0,"lose":2,"goalsFor":0,"goalsAgainst":6},"away":{"matchsPlayed":3,"win":0,"draw":1,"lose":2,"goalsFor":4,"goalsAgainst":6},"goalsDiff":-8,"points":1,"lastUpdate":"2020-10-19"}]]}}
If you change the definition of Api so standings is a list of lists it should work as the JSON has an array of arrays.
public partial class Api // ProductListing
{
public int results { get; set; }
public List<List<Standings>> standings { get; set; }
}

Deserialize shodan data

I'm having problems deserializing the data I'm getting from Shodan. Below are the classes I got from json2csharp and I'm trying to create an array of the matches and loop through them. It seems like I have tried with everything except a working array by now. The data itself is matches as root with objects of them that contain location (with its own data etc). An except below that I cut out a bit.
This is my error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Shodan.Match[]' because the type requ
ires a JSON array (e.g. [1,2,3]) to deserialize correctly.
var data = JsonConvert.DeserializeObject<Match[]>(allData);
{"matches": [{"product": "product", "hash": 0, "ip": 123123, "isp": "Verizon Internet Services"}], "total": 1}
public class Location
{
public string city { get; set; }
public string region_code { get; set; }
public object area_code { get; set; }
public double longitude { get; set; }
public string country_code3 { get; set; }
public double latitude { get; set; }
public string postal_code { get; set; }
public object dma_code { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
}
public class Options
{
}
public class Shodan
{
public string crawler { get; set; }
public string id { get; set; }
public string module { get; set; }
public Options options { get; set; }
}
public class Match
{
public int hash { get; set; }
public int ip { get; set; }
public string isp { get; set; }
public string transport { get; set; }
public string data { get; set; }
public string asn { get; set; }
public int port { get; set; }
public List<string> hostnames { get; set; }
public Location location { get; set; }
public DateTime timestamp { get; set; }
public List<string> domains { get; set; }
public string org { get; set; }
public object os { get; set; }
public Shodan _shodan { get; set; }
public string ip_str { get; set; }
public string product { get; set; }
}
public class RootObject
{
public List<Match> matches { get; set; }
public int total { get; set; }
}
You could create another class like
var allData =
{"matches": [{"product": "product", "hash": 0, "ip": 123123, "isp": "Verizon Internet Services"}], "total": 1}
public class MyMatches {
public Match[] matches {get; set;}
}
and then use that in the deserializer.
var data = JsonConvert.DeserializeObject<MyMatches>(allData);
This is if the JSON code sample you gave us is correct.
CORRECTION
Just saw the RootObject class.
Just use that.

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"

Deserializing JSON objects, keep getting errors (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

Windows 8 C#/XAML SuspensionManager failed

I'm writing simple imageviewer for one imageboard. I'm using these 2 classes for navigation(navigation parameter for Frame.Navigate() method) in my app:
public class KonaParameter
{
public int page { get; set; }
public string tags { get; set; }
public KonaParameter()
{
page = 1;
}
}
public class Post
{
public int id { get; set; }
public string tags { get; set; }
public int created_at { get; set; }
public int creator_id { get; set; }
public string author { get; set; }
public int change { get; set; }
public string source { get; set; }
public int score { get; set; }
public string md5 { get; set; }
public int file_size { get; set; }
public string file_url { get; set; }
public bool is_shown_in_index { get; set; }
public string preview_url { get; set; }
public int preview_width { get; set; }
public int preview_height { get; set; }
public int actual_preview_width { get; set; }
public int actual_preview_height { get; set; }
public string sample_url { get; set; }
public int sample_width { get; set; }
public int sample_height { get; set; }
public int sample_file_size { get; set; }
public string jpeg_url { get; set; }
public int jpeg_width { get; set; }
public int jpeg_height { get; set; }
public int jpeg_file_size { get; set; }
public string rating { get; set; }
public bool has_children { get; set; }
public object parent_id { get; set; }
public string status { get; set; }
public int width { get; set; }
public int height { get; set; }
public bool is_held { get; set; }
public string frames_pending_string { get; set; }
public List<object> frames_pending { get; set; }
public string frames_string { get; set; }
public List<object> frames { get; set; }
public object flag_detail { get; set; }
}
The problem I faced is that suspending doesn't work. SuspensionManager throws "SuspensionManager failed" exception after await SuspensionManager.SaveAsync(); call(I googled that it's because of using complex types).
I tried to use string as a navigation parameter. It works, but I need more than 1 string for my parameter(List<string> doesn't work, I tried to use it).
How to suspend my app correctly?
The problem is that SuspensionManager uses Frame.GetNavigationState() to get the history of the Frame. It then tries to serialise the navigation history to a string, unfortunately it has no way to know how to serialise custom complex types as parameters like Post or KonaParameter and fails with an exception.
If you want to use SuspensionManager then you'll need to restrict yourself to simple types like int or string as parameters.
If you do need complex types then it's best to store them in some background service / repository with an identifier. You can then pass the identifier as the parameter.
The recommended approach is to serialize your "state" object, so it can be saved/restored as a string. You can use Json.NET to do this:
//save state
Post post = //...;
string state = JsonConvert.Serialize(post)
//restore state
Post post = JsonConvert.Deserialize<Post>(state);
If you want to use two objects (a Post and a KonaParameter), I'd consider creating an "aggregate" class that encapsulates both, and serializing/deserializing that instead:
public class State
{
public Post post {get; set;}
public KonaParameter param {get; set;}
}

Categories