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