反序列化JSON对象,不断收到错误(C#)

时间:2017-06-03 09:03:20

标签: c# json

我正在尝试反序列化我的json代码。 json代码是一个字符串,json代码看起来像这样(所以我假设它是json对象)

{
  "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"
}

如何将“message”容器放在字符串中?这样字符串将包含“testzilla - 2133746943A9”,不带引号。我正在使用JSON.Net
包含此json代码的字符串的名称是“MACs”。在此先感谢。

PS:我是一名新的编码员。

4 个答案:

答案 0 :(得分:1)

json文件开头缺少“{”,请尝试添加

答案 1 :(得分:0)

您需要创建c#类来反序列化您的json字符串。根据你的json结构,我创建了你的课程,如下所示

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

无需使用库JSON.Net。您只需使用System.Web.Script.Serialization反序列化json字符串即可完成此操作。

注意:System.Web.Script.Serialization名称空间内可以使用System.Web.Extensions

以下是完整的代码。我将您的json数据保存在名为“test2.json”的文件中,并从该文件中使用它。

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

输出

enter image description here

答案 2 :(得分:0)

您可以使用正则表达式获取所需的值。

{{1}}

答案 3 :(得分:0)

您还可以使用动态类型进行反序列化。您无需为反序列化编写对象:

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

输出:

  

帖子ID:13