C# 无法从Json C获取特定字符串#

C# 无法从Json C获取特定字符串#,c#,json,C#,Json,这就是我的json的样子: 我创建了以下代码: public class user { public string username { get; set; } public int userid { get; set; } public string red { get; set; } public string acompaccompelted_setup

这就是我的json的样子:

我创建了以下代码:

public class user
{
    public string username { get; set; }
    public int userid
    {
        get;
        set;
    }
    public string red
    {
        get;
        set;
    }

    public string acompaccompelted_setup
    {
        get;
        set;
    }
}
string url = "localhost/testingdata/file.json";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
string jsonValue = "";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
{
    StreamReader reader = new StreamReader(response.GetResponseStream());
    json = reader.ReadToEnd();
}

user  listing = JsonConvert.DeserializeObject<user>(json);

Console.WriteLine(listing.username);
Console.ReadLine();
为了从URL获取数据,我使用了以下代码:

public class user
{
    public string username { get; set; }
    public int userid
    {
        get;
        set;
    }
    public string red
    {
        get;
        set;
    }

    public string acompaccompelted_setup
    {
        get;
        set;
    }
}
string url = "localhost/testingdata/file.json";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
string jsonValue = "";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
{
    StreamReader reader = new StreamReader(response.GetResponseStream());
    json = reader.ReadToEnd();
}

user  listing = JsonConvert.DeserializeObject<user>(json);

Console.WriteLine(listing.username);
Console.ReadLine();

然后我得到了JSON的完整列表。但我只想提取用户名。我试图按照这里给出的步骤进行操作,但没有成功。我做错了什么?

定义一个包装器
UserInfo
类来表示整个对象,如下所示:

class UserInfo
{
    [JsonProperty(PropertyName = "user", NullValueHandling = NullValueHandling.Ignore)]
    public User User { get; set; }
}
class User
{
    [JsonProperty(PropertyName = "userName", NullValueHandling = NullValueHandling.Ignore)]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "userId", NullValueHandling = NullValueHandling.Ignore)]
    public long  UserId { get; set; }
    [JsonProperty(PropertyName = "accompletedSetup", NullValueHandling = NullValueHandling.Ignore)]
    public string AccompletedSetup { get; set; }
    [JsonProperty(PropertyName = "accompletedInfo", NullValueHandling = NullValueHandling.Ignore)]
    public AccompletedInfo AccompletedInfo { get; set; }
}
class AccompletedInfo
{
}
var user = JsonConvert.DeserializeObject<UserInfo>(json);
定义
用户
类,如下所示:

class UserInfo
{
    [JsonProperty(PropertyName = "user", NullValueHandling = NullValueHandling.Ignore)]
    public User User { get; set; }
}
class User
{
    [JsonProperty(PropertyName = "userName", NullValueHandling = NullValueHandling.Ignore)]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "userId", NullValueHandling = NullValueHandling.Ignore)]
    public long  UserId { get; set; }
    [JsonProperty(PropertyName = "accompletedSetup", NullValueHandling = NullValueHandling.Ignore)]
    public string AccompletedSetup { get; set; }
    [JsonProperty(PropertyName = "accompletedInfo", NullValueHandling = NullValueHandling.Ignore)]
    public AccompletedInfo AccompletedInfo { get; set; }
}
class AccompletedInfo
{
}
var user = JsonConvert.DeserializeObject<UserInfo>(json);
AccompledInfo
属性是一个复杂的对象。因此,定义一个新类来表示它,如下所示:

class UserInfo
{
    [JsonProperty(PropertyName = "user", NullValueHandling = NullValueHandling.Ignore)]
    public User User { get; set; }
}
class User
{
    [JsonProperty(PropertyName = "userName", NullValueHandling = NullValueHandling.Ignore)]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "userId", NullValueHandling = NullValueHandling.Ignore)]
    public long  UserId { get; set; }
    [JsonProperty(PropertyName = "accompletedSetup", NullValueHandling = NullValueHandling.Ignore)]
    public string AccompletedSetup { get; set; }
    [JsonProperty(PropertyName = "accompletedInfo", NullValueHandling = NullValueHandling.Ignore)]
    public AccompletedInfo AccompletedInfo { get; set; }
}
class AccompletedInfo
{
}
var user = JsonConvert.DeserializeObject<UserInfo>(json);
对所有嵌套对象遵循相同的模式。i、 e.为每个嵌套对象定义一个类,在
JsonProperty
属性中使用属性和属性名称

然后使用
jsonvert
反序列化,如下所示:

class UserInfo
{
    [JsonProperty(PropertyName = "user", NullValueHandling = NullValueHandling.Ignore)]
    public User User { get; set; }
}
class User
{
    [JsonProperty(PropertyName = "userName", NullValueHandling = NullValueHandling.Ignore)]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "userId", NullValueHandling = NullValueHandling.Ignore)]
    public long  UserId { get; set; }
    [JsonProperty(PropertyName = "accompletedSetup", NullValueHandling = NullValueHandling.Ignore)]
    public string AccompletedSetup { get; set; }
    [JsonProperty(PropertyName = "accompletedInfo", NullValueHandling = NullValueHandling.Ignore)]
    public AccompletedInfo AccompletedInfo { get; set; }
}
class AccompletedInfo
{
}
var user = JsonConvert.DeserializeObject<UserInfo>(json);
var user=JsonConvert.DeserializeObject(json);

对象
user
现在具有预期的所有属性。

定义一个包装器
UserInfo
类来表示整个对象,如下所示:

class UserInfo
{
    [JsonProperty(PropertyName = "user", NullValueHandling = NullValueHandling.Ignore)]
    public User User { get; set; }
}
class User
{
    [JsonProperty(PropertyName = "userName", NullValueHandling = NullValueHandling.Ignore)]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "userId", NullValueHandling = NullValueHandling.Ignore)]
    public long  UserId { get; set; }
    [JsonProperty(PropertyName = "accompletedSetup", NullValueHandling = NullValueHandling.Ignore)]
    public string AccompletedSetup { get; set; }
    [JsonProperty(PropertyName = "accompletedInfo", NullValueHandling = NullValueHandling.Ignore)]
    public AccompletedInfo AccompletedInfo { get; set; }
}
class AccompletedInfo
{
}
var user = JsonConvert.DeserializeObject<UserInfo>(json);
定义
用户
类,如下所示:

class UserInfo
{
    [JsonProperty(PropertyName = "user", NullValueHandling = NullValueHandling.Ignore)]
    public User User { get; set; }
}
class User
{
    [JsonProperty(PropertyName = "userName", NullValueHandling = NullValueHandling.Ignore)]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "userId", NullValueHandling = NullValueHandling.Ignore)]
    public long  UserId { get; set; }
    [JsonProperty(PropertyName = "accompletedSetup", NullValueHandling = NullValueHandling.Ignore)]
    public string AccompletedSetup { get; set; }
    [JsonProperty(PropertyName = "accompletedInfo", NullValueHandling = NullValueHandling.Ignore)]
    public AccompletedInfo AccompletedInfo { get; set; }
}
class AccompletedInfo
{
}
var user = JsonConvert.DeserializeObject<UserInfo>(json);
AccompledInfo
属性是一个复杂的对象。因此,定义一个新类来表示它,如下所示:

class UserInfo
{
    [JsonProperty(PropertyName = "user", NullValueHandling = NullValueHandling.Ignore)]
    public User User { get; set; }
}
class User
{
    [JsonProperty(PropertyName = "userName", NullValueHandling = NullValueHandling.Ignore)]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "userId", NullValueHandling = NullValueHandling.Ignore)]
    public long  UserId { get; set; }
    [JsonProperty(PropertyName = "accompletedSetup", NullValueHandling = NullValueHandling.Ignore)]
    public string AccompletedSetup { get; set; }
    [JsonProperty(PropertyName = "accompletedInfo", NullValueHandling = NullValueHandling.Ignore)]
    public AccompletedInfo AccompletedInfo { get; set; }
}
class AccompletedInfo
{
}
var user = JsonConvert.DeserializeObject<UserInfo>(json);
对所有嵌套对象遵循相同的模式。i、 e.为每个嵌套对象定义一个类,在
JsonProperty
属性中使用属性和属性名称

然后使用
jsonvert
反序列化,如下所示:

class UserInfo
{
    [JsonProperty(PropertyName = "user", NullValueHandling = NullValueHandling.Ignore)]
    public User User { get; set; }
}
class User
{
    [JsonProperty(PropertyName = "userName", NullValueHandling = NullValueHandling.Ignore)]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "userId", NullValueHandling = NullValueHandling.Ignore)]
    public long  UserId { get; set; }
    [JsonProperty(PropertyName = "accompletedSetup", NullValueHandling = NullValueHandling.Ignore)]
    public string AccompletedSetup { get; set; }
    [JsonProperty(PropertyName = "accompletedInfo", NullValueHandling = NullValueHandling.Ignore)]
    public AccompletedInfo AccompletedInfo { get; set; }
}
class AccompletedInfo
{
}
var user = JsonConvert.DeserializeObject<UserInfo>(json);
var user=JsonConvert.DeserializeObject(json);

对象
user
现在具有预期的所有属性。

我尝试应用该建议,但现在我得到这一行“var user=JsonConvert.DeserializeObject(jsonValue);”以下错误:其他信息:无法将字符串转换为整数:85860803272478784。路径“user.userid”,第1行,位置135。。我尝试应用Convert.ToInt(32)请使用更大的数据类型,如
long
作为
UserId
属性。天哪!现在它工作了!你是个明星!我花了好几天来处理它!我从你的评论中学到了很多!请允许我再问你一个问题,因为这是我第一次处理JSON并大量阅读,但似乎我仍然错过了一些东西。前一个很有魅力。我试图访问这个复杂对象,我这样做是为了遵循您的建议:public accountedinfo accountedinfo{get;set;}class accountedinfo{[JsonProperty(PropertyName=“completed”,NullValueHandling=NullValueHandling.Ignore)]public string completed{get;set;}var user=JsonConvert.DeserializeObject(json);但我得到了0值。为什么?我试图应用该建议,但现在我得到了这一行“var user=JsonConvert.DeserializeObject(jsonValue)”;以下错误:附加信息:无法将字符串转换为整数:85860803271478784.Path“user.userid”,第1行,位置135..我试图应用convert.ToInt(32)请为
UserId
属性使用更大的数据类型,如
long
。天哪!现在它工作了!你是个明星!我花了好几天时间处理它!我从你的评论中学到了很多!我可以再问你一个问题吗?因为这是我第一次处理JSON并阅读了很多内容,但似乎我仍然错过了一些东西。前一个问题很有效我试图访问这个复杂的对象,我这样做是为了遵循你的建议:public AccompletedInfo AccompletedInfo{get;set;}class AccompletedInfo{[JsonProperty(PropertyName=“completed”,NullValueHandling=NullValueHandling.Ignore)]public string completed{get;set;}var user=JsonConvert.DeserializeObject(json);但我得到了0值。为什么?