【问题标题】:parse nested json string in c#在 C# 中解析嵌套的 json 字符串
【发布时间】:2014-11-07 07:20:50
【问题描述】:

我有 json 字符串作为

{"AccountNo":"345234533466","AuthValue":"{\"TopUpMobileNumber\":\"345234533466\",\"VoucherAmount\":\"100\"}"}

为了解析这个字符串,我创建了一个类

public class UserContext
{
    public string AccountNo { get; set; }
    public string AuthValue { get; set; }
}

在 AuthValue 中,它给我的输出为{\"TopUpMobileNumber\":\"345234533466\",\"VoucherAmount\":\"100\"},这是绝对正确的。现在我想修改我的类,我希望AuthValue 也采用字符串格式和单独的成员变量格式。

所以我以这种方式修改了我的课程,但它给出了错误

public class UserContext
{
    public string AccountNo { get; set; }
    public string AuthValue { get; set; }
    public Auth ????? { get; set; }
}

 public class Auth
{
    public string TopUpMobileNumber { get; set; }
    public string VoucherAmount { get; set; }
}

我的要求是

  1. 我需要 AuthValue 整个 json 字符串
  2. 在另一个变量中我想要成员明智的值

解析逻辑

UserContext conObj1 = new UserContext();
conObj1 = JsonConvert.DeserializeObject<UserContext>(context);

注意:不允许修改 json 字符串。

【问题讨论】:

  • 嗯,是的,它会给你一个错误 - 一方面是因为你试图声明两个具有相同名称的成员。但它很容易修复,至少使用 JSON.NET。 (不幸的是,我现在没有时间发布答案......)
  • 事情是,如果我修改我的变量名,我将无法解析 json 字符串
  • 您尝试使用单个类来表示两种不同的事物——一种是字符串,另一种是对象。我们不知道您使用什么进行 JSON 解析,这无济于事。
  • @JonSkeet 我正在使用JsonConvert
  • 顺便说一句,使用SomeType x = new SomeType(); x = someExpression; 几乎总是毫无意义的。只需使用SomeType x = someExpression;。为什么要创建不需要的对象?

标签: c# json


【解决方案1】:

我对 JsonConvert 或 Json.NET 不是很熟悉,所以我不确定有哪些可用的选项。就我个人而言,我会在之后立即再次调用反序列化器。

UserContext conObj1 = new UserContext();
conObj1 = JsonConvert.DeserializeObject<UserContext>(context);
conObj1.AuthObject = JsonConvert.DeserializeObject<Auth>(conObj1.AuthValue);

如果你愿意,你可以把它移到类中并直接从反序列化的类中调用它。

public class UserContext
{
    public string AccountNo { get; set; }
    public string AuthValue { get; set; }
    public Auth AuthObject { get; private set; }

    internal UserContext Deserialize()
    {
        // Serialize the object
        this.AuthObject = JsonConvert.DeserializeObject<Auth>(this.AuthValue);

        // Return this object for a simple single-line call.
        return this;
    }
}

// Single object
UserContext conObj1 = new UserContext();
conObj1 = JsonConvert.DeserializeObject<UserContext>(context).Deserialize();

// Enumeration of object (given that this is supported in JsonConvert)
IEnumerable<UserContext> conObjs = JsonConvert.DeserializeObject<IEnumerable<UserContext>(contexts).Select(c => c.Deserialize()).ToList();

或者,如果您觉得自己讨厌,您可以在访问属性时进行反序列化(尽管由于它可能导致许多问题,我会不惜一切代价避免这种情况)。

public class UserContext
{
    private Auth m_auth;

    public string AccountNo { get; set; }
    public string AuthValue { get; set; }
    public Auth AuthObject
    {
        get
        {
            if (this.m_auth == null)
            {
                this.m_auth = JsonConvert.DeserializeObject<Auth>(this.AuthValue);
            }

            return this.m_auth;
        }
    }
}

【讨论】:

    【解决方案2】:

    我建议使用两个类 - 一个用于您实际接收的 JSON,另一个用于您想要使用的对象模型:

    public class JsonUserContext
    {
        public string AccountNo { get; set; }
        public string AuthValue { get; set; }
    }
    
    public class UserContext
    {
        public string AccountNo { get; set; }
        public Auth AuthValue { get; set; }
    }
    
    public class Auth
    {
        public string TopUpMobileNumber { get; set; }
        public string VoucherAmount { get; set; }
    }
    
    ...
    
    var jsonUserContext = JsonConvert.DeserializeObject<JsonUserContext>(json);
    var authJson = jsonUserContext.AuthValue;
    var userContext = new UserContext {
        AccountNo = jsonUserContext.AccountNo,
        AuthValue = JsonConvert.DeserializeObject<JsonUserContext>(authJson);
    };
    

    【讨论】:

    • 我有一个类似的问题posted here。 (我环顾四周;直到现在都找不到任何类似的问题)。您认为现在有更好的方法来直接解析嵌入的 Json 字符串吗?我认为我的问题不是重复的。目前,我正在单独解析嵌入的 json 字符串。寻找更直接的方法。
    • @AdithyaUpadhya:不,我不这么认为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    相关资源
    最近更新 更多