douji2520 2019-07-30 09:54
浏览 53

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

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

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
    • ¥15 找一个网络防御专家,外包的
    • ¥100 能不能让两张不同的图片md5值一样,(有尝)
    • ¥15 informer代码训练自己的数据集,改参数怎么改
    • ¥15 请看一下,学校实验要求,我需要具体代码
    • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
    • ¥20 MATLAB绘制两隐函数曲面的交线
    • ¥15 求TYPCE母转母转接头24PIN线路板图
    • ¥100 国外网络搭建,有偿交流
    • ¥15 高价求中通快递查询接口