2016-09-23 10 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。 私はあなたのコードは次のようになります(私は、コード例外に関連し、JSON文字列をフォーマットしていないを削除した)私は、文字列

+0

は、あなたがデシリアライズJSONデータを、実際にははるかに簡単な –

+2

になるため、強く型付けされたクラスで動作するように計画して、あなたのJSON文字列を取り、それをコピーすることができ、視覚的なスタジオに移動し、編集を行います - >形式を選択して貼り付け - > JSONをクラスとして貼り付け –

答えて

1

上で解析したい私が間違っつもり場所を確認していない:

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"]