简体   繁体   English

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

[英]C# / Json.net / how can i deserialize the message?

please help me on what i need to do next?请帮助我下一步我需要做什么? i need this message to be inside their dedicated strings but i got no idea how to do it我需要将此消息放在他们的专用字符串中,但我不知道该怎么做

also how about using a dictionary?还有如何使用字典?

the message that i receive is this.. its inside a string called sServerResponse我收到的消息是这个..它在一个名为 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}}

please guide me.. countless googles ive made and topics i have read.请指导我.. 我制作的无数谷歌和我读过的主题。 the sample on json.net didnt even work json.net 上的样本甚至没有工作

EDIT/UPDATE:编辑/更新:

i'm now trying this one我现在正在尝试这个

List<WrapperReader> datas;

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

} }

but datas is empty, what may be the problem here?但是数据是空的,这里可能有什么问题?

UPDATE 2更新 2

i kind of figure whats making it to error我有点想知道是什么让它出错了

its the class.它的 class。

if im going to receive a array of strings.如果我要接收一个字符串数组。 then inside my class i must declare it as array to same goes to decimals and dates然后在我的 class 中,我必须将其声明为数组,以同样适用于小数和日期

about dates.. if im going to receive a date.关于日期..如果我要收到一个日期。 how do i declare it so it can contain a date?我如何声明它以便它可以包含日期?

im going to finish the coding see if its really the culprit我要完成编码,看看它是否真的是罪魁祸首

Hi你好
I wrote a basic example for you.我为你写了一个基本的例子。 You can use without Json.NET library.您可以在没有 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();
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM