简体   繁体   English

如何反序列化此JSON数组(C#)

[英]How do I deserialize this JSON array (C#)

I am struggling with a subject that has a lot of variants, but I can't seem to find one that works for me, and I think it's because of the way that my JSON array is. 我正在努力研究具有多种变体的主题,但是似乎找不到适合我的主题,我认为这是因为JSON数组的使用方式。 I'm not an expert in C# or JSON, but I already manage to "almost" get this to work. 我不是C#或JSON方面的专家,但我已经设法“几乎”使它起作用。 I need to get hand with the class that the JSON will deserialize to. 我需要处理JSON将反序列化到的类。

When I run the code I dont get an error, just a nulls in the xKisokData var. 当我运行代码时,我没有收到错误,xKisokData变量中只有一个空值。

The JSON data that I am getting. 我正在获取的JSON数据。 Their are these two different ones. 他们是这两个不同的人。

"{\"Event\": \"sConnection\",\"data[device]\": \"fb16f550-2ef1-11e5-afe9-ff37129acbf4\",\"data[mode]\": \"customer\",\"data[starttime]\": \"2015-07-22T16:07:42.030Z\",\"data[endtime]\": \"\"}"
"{\"Event\": \"Log\",\"data[id]\": \"2015-07-22T16:07:23.063Z\",\"data[messages][0][source]\": \"server\",\"data[messages][0][message]\": \"Server is listening on port 1553\"}"

The code I have so far: 我到目前为止的代码:

// Read in our Stream into a string...
StreamReader reader = new StreamReader(JSONdataStream);
string JSONdata = reader.ReadToEnd();

JavaScriptSerializer jss = new JavaScriptSerializer();
wsKisokData[] xKisokData = jss.Deserialize<wsKisokData[]>(JSONdata);

My Class: 我的课:

namespace JSONWebService
{

    [DataContract]
    [Serializable]
    public class KisokEvent
    {
        public string eventTrigger { get; set; }
    }

    [DataContract]
    [Serializable]
    public class KisokData
    {
        public string data { get; set; }

    }

    [DataContract]
    [Serializable]
    public class wsKisokData
    {
        public KisokEvent KDEvent { get; set; }
        public List<KisokData> KDData { get; set; }

    }

}

I am sure that I don't understand the Deserialize process. 我确定我不了解反序列化过程。 Thanks for the help. 谢谢您的帮助。

EDIT: I put the JSON in the top part right from the debugger, here is the strings. 编辑:我将JSON放在调试器的顶部,这是字符串。

{
    "Event": "sConnection",
    "data[device]": "fb16f550-2ef1-11e5-afe9-ff37129acbf4",
    "data[mode]": "customer",
    "data[starttime]": "2015-07-22T16:07:42.030Z",
    "data[endtime]": ""
}

{
    "Event": "Log",
    "data[id]": "2015-07-22T16:07:23.063Z",
    "data[messages][0][source]": "server",
    "data[messages][0][message]": "Server is listening on port 1553"
}

I would HIGHLY recommend using the json.net package off nuget instead. 我强烈建议使用nuget的json.net包代替。 You can generate template classes (models) for it by pasting the json into http://json2csharp.com/ 您可以通过将json粘贴到http://json2csharp.com/来为其生成模板类(模型)

Then use said models to convert the json into ac# object (deserializing) by doing a 然后使用上述模型通过执行以下操作将json转换为ac#对象(反序列化)

var jsonStructure = JsonConvert.DeserializeObject<model>(json)

And query as if it was just a standard object 并进行查询,就好像它只是一个标准对象

foreach (var x in jsonStructure.KDData)
{
    doAction(x.data);
}
// for example

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

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