Home > Back-end >  Parsing Json Data string And Retrieve Element
Parsing Json Data string And Retrieve Element

Time:11-16

My json is

 string aaaa = "{\"respCode\":\"1\",\"status\":\"SUCCESS\",\"response\":{\"chId\":1,\"refId\":\"YBL4ABC0D3D61A349F18605928DD63E8886\",\"approvalRefNum\":\"264992511\",\"responseCode\":\"000\",\"responseReason\":\"Successful\",\"complianceReason\":\"\",\"complianceRespCd\":\"\",\"billDetails\":\"[{\"name\":\"Consumer Number\",\"value\":\"900001073788\"}]\",\"billerResponse\":\"{\"customerName\":\"MNTALI\",\"amount\":\"421\",\"dueDate\":\"2021-11-06\",\"custConvFee\":\"\",\"custConvDesc\":\"\",\"billDate\":\"2021-10-16\",\"billNumber\":\"9337718556142\",\"billPeriod\":\"MONTHLY\",\"billTags\":[{\"name\":\"Early Payment Amount\",\"value\":\"41200\"}]}\",\"additionalInfo\":\"[{\"name\":\"Early Payment Date\",\"value\":\"2021-10-23\"},{\"name\":\"URL\",\"value\":\"https://cp.tatapower.com:4443/inv?inv_nou003dMDkzMzc3MTg2MTQy\"}]\"}}";

I want to retrieve the value of customerName.

When I tried something like this to parse my JSON string

  var userObj1 = JObject.Parse(aaaa);

I am getting Error such as:

'After parsing a value an unexpected character was encountered: n. Path 'response.billDetails', line 1, position 244.'

CodePudding user response:

its invalid JSON, remove " from before and after of '{','[','}',']'

string aaaa = 
              "{"  
                "\"respCode\":\"1\",\"status\":\"SUCCESS\",\"response\":"  
                "{"  
                "\"chId\":1,\"refId\":\"YBL4ABC0D3D61A349F18605928DD63E8886\",\"approvalRefNum\":\"264992511\",\"responseCode\":\"000\",\"responseReason\":\"Successful\",\"complianceReason\":\"\",\"complianceRespCd\":\"\",\"billDetails\":"  
                    "["  
                        "{\"name\":\"Consumer Number\",\"value\":\"900001073788\"}"  
                    "],"  
                    "\"billerResponse\":"  
                        "{"  
                            "\"customerName\":\"MNTALI\",\"amount\":\"421\",\"dueDate\":\"2021-11-06\",\"custConvFee\":\"\",\"custConvDesc\":\"\",\"billDate\":\"2021-10-16\",\"billNumber\":\"9337718556142\",\"billPeriod\":\"MONTHLY\",\"billTags\":"  
                            "["  
                                "{\"name\":\"Early Payment Amount\",\"value\":\"41200\"}"  
                            "]"  
                        "}"  
                    ",\"additionalInfo\":"  
                    "["  
                        "{\"name\":\"Early Payment Date\",\"value\":\"2021-10-23\"},"  
                        "{\"name\":\"URL\",\"value\":\"https://cp.tatapower.com:4443/inv?inv_nou003dMDkzMzc3MTg2MTQy\"}"  
                    "]"  
                  "}"  
                "}";

var userObj1 = JObject.Parse(aaaa);

its work for me

CodePudding user response:

You have an invalid json. Need to remove extra quotes at first

var json=aaaa.Replace("\"[","[").Replace("]\"","]").Replace("\"{","{").Replace("}\"","}");

var userObj1 = JObject.Parse(json);

output

{
  "respCode": "1",
  "status": "SUCCESS",
  "response": {
    "chId": 1,
    "refId": "YBL4ABC0D3D61A349F18605928DD63E8886",
    "approvalRefNum": "264992511",
    "responseCode": "000",
    "responseReason": "Successful",
    "complianceReason": "",
    "complianceRespCd": "",
    "billDetails": [
      {
        "name": "Consumer Number",
        "value": "900001073788"
      }
    ],
    "billerResponse": {
      "customerName": "MNTALI",
      "amount": "421",
      "dueDate": "2021-11-06",
      "custConvFee": "",
      "custConvDesc": "",
      "billDate": "2021-10-16",
      "billNumber": "9337718556142",
      "billPeriod": "MONTHLY",
      "billTags": [
        {
          "name": "Early Payment Amount",
          "value": "41200"
        }
      ]
    },
    "additionalInfo": [
      {
        "name": "Early Payment Date",
        "value": "2021-10-23"
      },
      {
        "name": "URL",
        "value": "https://cp.tatapower.com:4443/inv?inv_nou003dMDkzMzc3MTg2MTQy"
      }
    ]
  }
}
  • Related