2016-09-23 17 views
0

我想分析以下字符串,波纹管是我的代码,以下是我的字符串无法访问的孩子值Newtonsoft的Json

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。 我不知道我要去的地方错了,我想分析上面的字符串

+0

你可以计划使用强类型类的工作对反序列化JSON数据,这将是更简单 –

+2

其实,把你的JSON字符串,复制它,去到Visual Studio和做编辑 - >选择性粘贴 - >将JSON粘贴为类 –

回答

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属性,你会得到上面提到的异常。可能您需要从您的呼叫链中删除身份证。

jo["TaxProfile"]["IncomeTypeStatus"]