James Anthon
James Anthon

Reputation: 11

cannot access child value Newtonsoft Json

I want to parse following string , bellow is my code , and below is my string

  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>();

in last line i get error Cannot access child value on Newtonsoft.Json.Linq.JValue. I am not sure where i am going wrong i want to parse above string

Upvotes: 0

Views: 5124

Answers (1)

Artiom
Artiom

Reputation: 7837

Your code looks like (I've removed code not related to exception and formatted JSON string):

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>();

Code

jo["TaxProfile"]["id"]

returns 258658. So, if you try to get IncomeTypeStatus property of it, you'll get above mentioned exception. Probably you need to remove id from your call chain.

jo["TaxProfile"]["IncomeTypeStatus"]

Upvotes: 2

Related Questions