解析包含复杂结构的字符串

I'm receiving a third-party API payload response like:

{
    "message": "Validation failed because [{reason=CONDITIONAL_INVALID_VALUE, field=/targetingCriteria, batchIndex=0, type=INVALID_VALUE, message=/locale cannot be set to en if urn:li:adTargetingFacet:interfaceLocales is set to urn:li:locale:it_IT, parameters={field1=/locale, value2=urn:li:locale:it_IT, value1=en, field2=urn:li:adTargetingFacet:interfaceLocales, key=}}, {reason=FIELD_VALUE_TOO_LOW, field=dailyBudget, batchIndex=0, type=INVALID_VALUE, message=/dailyBudget/amount value 1 cannot be lower than 10.00, parameters={min=10.00, field=/dailyBudget/amount, costType=CPM, type=SPONSORED_UPDATES, value=1, key=}}]",
    "status": 400
}

and I'd like to transform in something like:

{
    "errors": [{
            "reason": "CONDITIONAL_INVALID_VALUE",
            "field": "/targetingCriteria",
            "batchIndex": "0",
            "type": "INVALID_VALUE",
            "message": "/locale cannot be set to en if urn:li:adTargetingFacet:interfaceLocales is set to urn:li:locale:it_IT",
            "parameters": "{field1=/locale, value2=urn:li:locale:it_IT, value1=en, field2=urn:li:adTargetingFacet:interfaceLocales, key=}"
        },
        {
            "reason": "FIELD_VALUE_TOO_LOW",
            "field": "dailyBudget",
            "batchIndex": "0",
            "type": "INVALID_VALUE",
            "message": "/dailyBudget/amount value 1 cannot be lower than 10.00",
            "parameters": "{min=10.00, field=/dailyBudget/amount, costType=CPM, type=SPONSORED_UPDATES, value=1, key=}"
        }
    ]
}

But I'm struggling to find a clear golang approach to this problem, the main problems are:

  1. no valid json is available: word are not correctly quoted with "
  2. = symbol instead of :
  3. nested graphs bracket

I'm currently try to transform in a valid json string and then parse as json but I have various problem with nested elements

Any idea?

EDIT: This is what I've done right now: https://play.golang.org/p/B7bdPCJoHc2

展开翻译

译文

我收到第三方API有效负载响应,例如:</ p>

   {
“ message”:“验证失败,因为如果urn:li:adTargetingFacet:interfaceLocales无法设置为[{reason = CONDITIONAL_INVALID_VALUE,field = / targetingCriteria,batchIndex = 0,type = INVALID_VALUE,message = / locale 设置为urn:li:locale:it_IT,参数= {field1 = / locale,value2 = urn:li:locale:it_IT,value1 = en,field2 = urn:li:adTargetingFacet:interfaceLocales,key =}},{原因 = FIELD_VALUE_TOO_LOW,字段= dailyBudget,batchIndex = 0,类型= INVALID_VALUE,消息= / dailyBudget /金额1不能低于10.00,参数= {min = 10.00,字段= / dailyBudget / amount,costType = CPM,类型= SPONSORED_UPDATES ,值= 1,键=}}]“”,
“状态”:400
}
</ code> </ pre>

,我想进行以下转换: </ p>

  {
“错误”:[{
“原因”:“ CONDITIONAL_INVALID_VALUE”,
“字段”:“ / targetingCriteria”,
“ batchIndex”: “ 0”,
“ type”:“ INVALID_VALUE”,
“ message”:“如果将urn:li:adTargetingFacet:interfaceLocales设置为urn:li:locale:it_IT,则无法将/ locale设置为en”,
“ parameters”:“ {field1 = / locale,value2 = urn:li:locale:it_IT,value1 = en,field2 = urn:li:adTargetingFacet:interfaceLocales,key =}”
},
{
“原因 “:” FIELD_VALUE_TOO_LOW“,
” field“:” dailyBudget“,
” batchIndex“:” 0“,
” type“:” INVALID_VALUE“,
” message“:” / dailyBudget /金额1 小于10.00“,
”参数“:” {min = 10.00,field = / dailyBudget / amount,costType = CPM,type = SPONSORED_UPDATES,value = 1,key =}“
}
]
}

</ code> </ pre>

但我一直在努力寻找解决此问题的明确golang方法,主要问题是:</ p>


  1. 没有可用的有效json:单词未用“ </ li>
  2. = </ code>”而不是:</ code> </ li>
    正确引用
  3. 嵌套图括号</ li >
    </ ol>

    我目前正在尝试转换为有效的json字符串,然后解析为json,但是嵌套元素存在各种问题</ p>

    有任何想法吗?</ p>

    编辑:</ strong>:这是我现在所做的: https://play.golang.org/p/B7bdPCJoHc2 </ p>
    </ div>

doushun4666
doushun4666 感谢@Tom的分享
10 个月之前 回复
douhuan1257
douhuan1257 在另一个问题中做了类似的事情。也许它也可以在这里提供帮助:stackoverflow.com/questions/57014004/…或stackoverflow.com/questions/57014004/…
10 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐