【问题标题】:cannot access child value Newtonsoft Json无法访问子值 Newtonsoft Json
【发布时间】:2017-01-31 20:02:03
【问题描述】:

我想解析下面的字符串,下面是我的代码,下面是我的字符串

  string jsn = Convert.ToString(
                                    @"{

                                'TaxProfile':{'id':258658,'IncomeTypeStatus':[{'IncomeType':'0001','StatusCodeDesc':'Ready For SAP','StatusCode':'RFS','PayFromCountryCode':'IE'}],'ExpirationDate':null,'FormName':null}, 

                                'ErrorJSON':'[{\'TypeID\':\'Z_FI_MDG\',\'SeverityCode\':\'3\',\'Note\':\'\\\'An Electronic Fund Transactions (EFT) routing number is comprised of a three-digit financial institution number and a five-digit branch number, preceded by a \\\\\\\'leading zero\\\\\\\'. \\\\\\\\r\\\\\\\\n•YYY: Institution\'}]'

                                        }"            
                                    );


    JObject jo = JObject.Parse(jsn);

   // dynamic jo = JObject.Parse(jsn);
    TenantPayeeMessage apTenantMessage = null;
  //  JObject jo = o;
  //  var auditObject = jo.ToString();

    JToken PartnerReferenceId;
    string Payeeid, PayeeStatus, bpid = string.Empty;
    JToken[] items = null;
    JToken sectionStatus = null;
    JToken TaxIncomType = null;
    JToken[] bank = null;
    var bankJson = new Dictionary<string, string>();

    JToken ErrorJSONSeverityNote,
            ErrorJSONSeverityCode,
            ErrorJSONTypID,
            BasicErrorJSON,
            Basicbpid,
            Basicstatus,
            BasicId, CompliancebpErrorJSON,
            Compliancebpid, Compliancestatus, ComplianceId, ErrorJSONpp,
            bbpidpp, statuspp,
            PaymentProfileId, FormName,
            ExpirationDate,
            PayFromCountryCode, StatusCode, StatusCodeDesc, IncomeType, TaxProfileId;

    //Guid SyncIdentifier = Guid.Parse(jo["BusinessPartnerSUITEBulkReplicateConfirmation"]["BusinessPartnerSUITEReplicateConfirmationMessage"]["MessageHeader"]["UUID"].Value<string>());


    if (null != jo["TaxProfile"]["id"] && null != jo["TaxProfile"]["id"])
    {
        TaxProfileId = jo["TaxProfile"]["id"].Value<string>();
    }

    TaxIncomType = jo["TaxProfile"]["id"]["IncomeTypeStatus"].Value<string>();

在最后一行我得到错误 无法访问 Newtonsoft.Json.Linq.JValue 上的子值。 我不确定我哪里出错了我想解析上面的字符串

【问题讨论】:

  • 你能否计划使用强类型类来反序列化 Json 数据,这样会更简单
  • 事实上,获取你的 json 字符串,复制它,去 Visual Studio 并执行编辑 -> 选择性粘贴 -> 将 JSON 粘贴为类

标签: c# asp.net json asp.net-mvc getjson


【解决方案1】:

您的代码看起来像(我已删除与异常和格式化 JSON 字符串无关的代码):

var jsn = Convert.ToString(
    @"{
        'TaxProfile': {
          'id': 258658,
          'IncomeTypeStatus': [
            {
              'IncomeType': '0001',
              'StatusCodeDesc': 'Ready For SAP',
              'StatusCode': 'RFS',
              'PayFromCountryCode': 'IE'
            }
          ],
          'ExpirationDate': null,
          'FormName': null
        },
        'ErrorJSON': '[{\'TypeID\':\'Z_FI_MDG\',\'SeverityCode\':\'3\',\'Note\':\'\\\'An Electronic Fund Transactions (EFT) routing number is comprised of a three-digit financial institution number and a five-digit branch number, preceded by a \\\\\\\'leading zero\\\\\\\'. \\\\\\\\r\\\\\\\\n•YYY: Institution\'}]'
        }");
var jo = JObject.Parse(jsn);

var TaxIncomType = jo["TaxProfile"]["id"]["IncomeTypeStatus"].Value<string>();

代码

jo["TaxProfile"]["id"]

返回 258658。因此,如果您尝试获取它的 IncomeTypeStatus 属性,您将得到上述异常。可能您需要从调用链中删除 id。

jo["TaxProfile"]["IncomeTypeStatus"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    相关资源
    最近更新 更多