Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t将JSon反序列化为C#_C#_Json_Winforms_Json.net - Fatal编程技术网

Can';t将JSon反序列化为C#

Can';t将JSon反序列化为C#,c#,json,winforms,json.net,C#,Json,Winforms,Json.net,我在我的windows窗体应用程序.net 4中返回了以下JSon,但我可以获得化身URL!尝试要么产生空值,要么在我当前的尝试中出现意外字符 以下是JSon: {"self":"http://jira.prod.xxxxxx.com/rest/api/2/user?username=firstname.lastname","key":"zzzzzz","name":"firstname.lastname","emailAddress":"firstname.lastname@xxxxxxxxx

我在我的windows窗体应用程序.net 4中返回了以下JSon,但我可以获得化身URL!尝试要么产生空值,要么在我当前的尝试中出现意外字符

以下是JSon:

{"self":"http://jira.prod.xxxxxx.com/rest/api/2/user?username=firstname.lastname","key":"zzzzzz","name":"firstname.lastname","emailAddress":"firstname.lastname@xxxxxxxxxx.com","avatarUrls":{"16x16":"http://jira.prod.xxxxxx.com/secure/useravatar?size=xsmall&ownerId=zzzzzz&avatarId=12500","24x24":"http://jira.prod.xxxxxx.com/secure/useravatar?size=small&ownerId=zzzzzz&avatarId=12500","32x32":"http://jira.prod.xxxxxx.com/secure/useravatar?size=medium&ownerId=zzzzzz&avatarId=12500","48x48":"http://jira.prod.xxxxxx.com/secure/useravatar?ownerId=zzzzzz&avatarId=12500"},"displayName":"Lastname, FirstName","active":true,"timeZone":"Europe/Dublin","locale":"en_UK","groups":{"size":1,"items":[]},"applicationRoles":{"size":1,"items":[]},"expand":"groups,applicationRoles"}
这是我当前获取URL的尝试,上面的json以“结果”形式传递: 我正在使用NewtonsoftJSON库

var response = JsonConvert.DeserializeObject<myselfResponse>(result);

 public class myselfResponse
{
    [JsonProperty("username")]
    public string username { get; set; }

    [JsonProperty("key")]
    public string key { get; set; }

    [JsonProperty("name")]
    public string name { get; set; }

    [JsonProperty("emailAddress")]
    public string emailAddress { get; set; }

    //public AvatarUrls avatarUrls { get; set; }

    [JsonProperty("avatarUrls")]
    public string avatarUrls { get; set; }
}

public class AvatarUrls
{
    public string _16x16 { get; set; }
    public string __invalid_name__24x24 { get; set; }
    public string __invalid_name__32x32 { get; set; }
    public string __invalid_name__48x48 { get; set; }
}
var response=JsonConvert.DeserializeObject(结果);
公共类自我响应
{
[JsonProperty(“用户名”)]
公共字符串用户名{get;set;}
[JsonProperty(“密钥”)]
公共字符串密钥{get;set;}
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“电子邮件地址”)]
公共字符串电子邮件地址{get;set;}
//公共化身化身{get;set;}
[JsonProperty(“avatarUrls”)]
公共字符串avatarUrls{get;set;}
}
公共类化身
{
公共字符串_16x16{get;set;}
公共字符串uu无效uu名称uuu 24x24{get;set;}
公共字符串uuu无效uuu32x32{get;set;}
公共字符串\uuuu无效\u名称\uuuu48x48{get;set;}
}
我得到的错误是意外字符


任何帮助都将非常感谢…

您似乎已注释掉类型为
AvatarUrls
的正确属性,并将其替换为字符串属性。不要那样做

正如您已经发现的,您可以使用
JsonProperty
属性将JSON属性的属性名设置为任何内容。因此,取消对该行的注释,删除额外的字符串属性,并对“无效名称”属性执行以下操作:

[JsonProperty("16x16")]
public string Size16x16 { get; set; }
或者,如果希望它是动态的,则根本不要使用单独的对象,并执行以下操作:

[JsonProperty("avatarUrls")]
public Dictionary<string, string> AvatarUrls { get; set;}
[JsonProperty(“avatarUrls”)]
公共字典AvatarUrls{get;set;}

请注意,通过使用
JsonProperty
您还可以在C#中使用正确的命名,并将属性名称大写。

您似乎已经注释掉了类型
AvatarUrls
的正确属性,并将其替换为字符串属性。不要那样做

正如您已经发现的,您可以使用
JsonProperty
属性将JSON属性的属性名设置为任何内容。因此,取消对该行的注释,删除额外的字符串属性,并对“无效名称”属性执行以下操作:

[JsonProperty("16x16")]
public string Size16x16 { get; set; }
或者,如果希望它是动态的,则根本不要使用单独的对象,并执行以下操作:

[JsonProperty("avatarUrls")]
public Dictionary<string, string> AvatarUrls { get; set;}
[JsonProperty(“avatarUrls”)]
公共字典AvatarUrls{get;set;}
请注意,通过使用
JsonProperty
,您还可以在C#中使用正确的命名,并将属性名称大写