C#/ Json.net /如何反序列化消息?

时间:2011-05-04 07:24:56

标签: c# dictionary json.net deserialization

请帮助我接下来需要做什么?我需要这条消息在他们的专用字符串中 但我不知道该怎么做

使用字典怎么样?

我收到的消息是这个..它在一个名为sServerResponse

的字符串里面
{"pcname": "Kat", "pause": [], "ip": "192.168.1.100", "paused": 0, "services": [], "session": "", "shop": [], "end": 0, "used": 0, "start": 0, "timeup": 0, "follow": "sui", "consq": {"1": "basic", "3": "rounding", "2": "minimum"}, "stas": 0, "price": 0, "pc_status": 1, "account_details": {"3": "Member", "2": "Staff/Member", "4": "Timecode"}, "msg": "Connection/Update accepted.", "mac": "00112233445", "others": [], "remarks": "", "ticker": {"units": 1}, "fixed_time": [], "account": "", "pause_tmp": {"s": 0, "e": 0}, "ver": {}, "status_details": {"1": "Idle", "0": "Offline", "5": "Fixed", "4": "Open", "7": "Mem", "6": "Extended", "8": "Timecode"}, "cmd": "nothing", "load": 0, "deposit": [], "data": {"fixed": [], "open": 0}, "restirctions": {"start_button": 0, "keys": [{"ctrl-esc": 0}, {"alt-tab": 0}], "mouse": 1, "drives": [{"c": 0}, {"d": 0}], "desktop": 0, "task_manager": 0, "keyboard": 1, "taskbar": 0, "control_panel": 0}}

请指导我..我做过无数谷歌和我读过的主题。 json.net上的示例甚至没有工作

EDIT / UPDATE:

我现在正在尝试这个

List<WrapperReader> datas;

 datas = new JavaScriptSerializer(new SimpleTypeResolver()).Deserialize<List<WrapperReader>>(jsonInput);

}

但是数据是空的,这可能是什么问题?

更新2

我有什么想法让它出错

它的班级。

如果我要接收一个字符串数组。然后在我的班级里面我必须将它声明为数组 小数和日期也一样

关于约会..如果即将收到约会。如何声明它以便它可以包含日期?

即将完成编码,看看它真的是罪魁祸首

1 个答案:

答案 0 :(得分:0)

你好 我为你写了一个基本的例子。 没有Json.NET库就可以使用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace ConsoleApplication1
{
public class Customer
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public string CustomerSurname { get; set; }

    public override string ToString()
    {
        return this.CustomerID + " " + this.CustomerName + " " + this.CustomerSurname;
    }
}

 class Program
 {
    static void Main(string[] args)
    {
        string JSonData = @"[
        {
             ""CustomerID"": ""1"",
             ""CustomerName"": ""Mehmet"",
             ""CustomerSurname"": ""Tasköprü""
        },
        {
             ""CustomerID"": ""2"",
             ""CustomerName"": ""Cin"",
             ""CustomerSurname"": ""Ali""
        },
        {
             ""CustomerID"": ""3"",
             ""CustomerName"": ""Temel"",
             ""CustomerSurname"": ""Reis""
        }]";

        IList<Customer> iListData = new JavaScriptSerializer().Deserialize<IList<Customer>>(JSonData);

        foreach (var item in iListData)
            Console.WriteLine(item.ToString());

        Console.Read();
    }
}

}