解析 Json 数据字符串并检索元素

Posted

技术标签:

【中文标题】解析 Json 数据字符串并检索元素【英文标题】:Parsing Json Data string And Retrieve Element 【发布时间】:2021-12-26 16:27:36 【问题描述】:

我的json是

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

我想检索 customerName 的值。

当我尝试这样的方法来解析我的 JSON 字符串时

  var userObj1 = JObject.Parse(aaaa);

我收到如下错误:

'解析一个值后遇到了一个意外字符:n.路径“response.billDetails”,第 1 行,位置 244。

【问题讨论】:

这是一个无效的 JSON,它在 billDetailsadditionalInfo 数组和 billerResponse 对象值周围有多余的双引号。 对于这样的未来问题:获取您的 json 字符串(并将 \" 替换为 ")并将其粘贴到 json 验证器中,例如 jsonformatter.curiousconcept.com/# 【参考方案1】:

你有一个无效的 json。首先需要删除多余的引号

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

var userObj1 = JObject.Parse(json);

输出


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

【讨论】:

【参考方案2】:

它的 JSON 无效,去掉 '','[','',']' 前后的 ""

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

它对我有用

【讨论】:

以上是关于解析 Json 数据字符串并检索元素的主要内容,如果未能解决你的问题,请参考以下文章

使用 swift 将 JSON 检索到的 int32 解析为核心数据类

解析 JSON 数据时无法将字符串附加到数组

jquery数据属性不解析json字符串

如何从 JSON 数组中检索字符串数组?

Javascript - 解析 html 字符串获取具有类 X 的每个元素并返回它们的数组

mysql中的json数据类型