Kevin
Kevin

Reputation: 3

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.

Upvotes: 1

Views: 93

Answers (4)

Timur Lemeshko
Timur Lemeshko

Reputation: 2879

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

Upvotes: 0

Youssef13
Youssef13

Reputation: 4944

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

Upvotes: 0

Chandan Kumar
Chandan Kumar

Reputation: 4638

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

enter image description here

Upvotes: 0

Benoit Sepe
Benoit Sepe

Reputation: 33

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

Upvotes: 1

Related Questions