简体   繁体   English

使用 C# 使用 JSON 进行反序列化的问题

[英]Issue with Deserialization with JSON with C#

I have looked over example after example after example and none of my attempts have worked.我在一个又一个例子中查看了一个例子,但我的尝试都没有奏效。

I'm attempting to deserialize this JSON return:我正在尝试反序列化这个 JSON 返回:

{
  "status": "success",
  "data": {
    "result": "match",
    "id_user": 26564,
    "dob_match": null,
    "first_name_match": null,
    "last_name_match": null
  },
  "code": 200
}

Here is my JSON object class declaration:这是我的 JSON 对象类声明:

[DataContract]
internal class DccCoachApi
{
    [DataMember]
    public string result { get; set; }
    public string id_user { get; set; }
    public string dob_match { get; set; }
    public string first_name_match { get; set; }
    public string last_name_match { get; set; }
}

In my stream method, my streamRead variable is filled with: {"status":"success","data":{"result":"match","id_user":26564,"dob_match":null,"first_name_match":null,"last_name_match":null},"code":200}在我的流方法中,我的 streamRead 变量填充有: {"status":"success","data":{"result":"match","id_user":26564,"dob_match":null,"first_name_match":null,"last_name_match":null},"code":200}

Method 1 does not populate coachId:方法 1 不填充 CoachId:

            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(streamRead)))
            {
                // Deserialization from JSON  
                var deserializer = new DataContractJsonSerializer(typeof(DccCoachApi));
                var dccObj = (DccCoachApi)deserializer.ReadObject(ms);
                coachId = dccObj.id_user;
            }

Nor does method 2:方法 2 也不行:

            DccCoachApi coach = new JavaScriptSerializer().Deserialize<DccCoachApi>(streamRead);

            coachId = coach.id_user;

nor does method 3:方法 3 也不行:

            JavaScriptSerializer js = new JavaScriptSerializer();
            DccCoachApi dccObj = js.Deserialize<DccCoachApi>(streamRead);
            coachId = dccObj.id_user;

nor does method 4:方法 4 也不行:

            dynamic dccObject = js.Deserialize<dynamic>(streamRead);
            coachId = dccObject["id_user"];

The hard error that gets produced when i pull the value directly off method 4 is: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.当我直接从方法 4 中提取值时产生的硬错误是: System.Collections.Generic.KeyNotFoundException:字典中不存在给定的键。 at System.Collections.Generic.Dictionary`2.get_Item(TKey key)在 System.Collections.Generic.Dictionary`2.get_Item(TKey 键)

Methods 1-3 do not hit a hard error, however they populate coachId with no data.方法 1-3 不会遇到硬错误,但是它们会在没有数据的情况下填充 CoachId。

Can somebody please let me know what i'm doing wrong?有人可以让我知道我做错了什么吗?

You can simply generate proper classes here: http://json2csharp.com/您可以在这里简单地生成适当的类: http : //json2csharp.com/

This is how it should look like you don't need the DataMember Attributes, it might confuse the serializer to only de-serialize this single property:这就是您不需要 DataMember 属性的样子,它可能会混淆序列化程序只反序列化这个单个属性:

public class Data
{
    public string result { get; set; }
    public int id_user { get; set; }
    public object dob_match { get; set; }
    public object first_name_match { get; set; }
    public object last_name_match { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public Data data { get; set; }
    public int code { get; set; }
}

Code:代码:

var deserializer = DataContractJsonSerializer(typeof(RootObject));
var root = (RootObject)deserializer.ReadObject(ms);
var coachId = root.data.id_user;

I revised the code to look like this, and it is dumping my value perfectly.我将代码修改成这样,它完美地倾销了我的价值。 Thanks much to everyone who helped me reach the solution.非常感谢所有帮助我找到解决方案的人。 Plutonix, thanks as well for the paste special 411. I had no idea that existed. Plutonix,也感谢粘贴特殊的 411。我不知道存在。 VERY useful!很有用!

                using (var reader = new StreamReader(webResponse.GetResponseStream()))
            {
                var streamRead = reader.ReadToEnd();
                reader.Close();
                using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(streamRead)))
                {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    DccCoachRootobject dccObj = js.Deserialize<DccCoachRootobject>(streamRead);
                    coachId = dccObj.data.id_user.ToString();
                }
            }

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

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