1

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.

9
  • Welcome to StackOverflow :) Reply/Provide comments whenever you got any answer or any comments to your question Commented Jun 3, 2017 at 9:16
  • Did misterbenoit answer helped you to solve your problem? Commented Jun 3, 2017 at 9:17
  • @uɐpuɐɥƆ no, I just pasted the code incorrectly. Thanks though. Question remains the same, how do I create a string which contains everything that "message" contains? Commented Jun 3, 2017 at 9:34
  • you want this json string to be converted to c#? Commented Jun 3, 2017 at 9:36
  • You say you "keep getting errors" - but you haven't shown us either the code you're trying or the error you're getting, which makes it very hard to help you. Please provide a minimal reproducible example. Commented Jun 3, 2017 at 9:36

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

Thank you so much! I will try this out and I will get back to you
I am getting some new errors now in "HttpResponseExtensions.cs". Screenshot: prntscr.com/ffdxh7 prntscr.com/ffdxrb @uɐpuɐɥƆ
try with a new console application referring my answer. It works perfectly. then you can integrate this with your application. the error might be because of some other reason, u need to debug and find where the error is appearing
I clicked it and I got this message: prntscr.com/ffe6yc @uɐpuɐɥƆ Thanks to your help I was able to successfully create the entire application. Thank you.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.