简体   繁体   English

JSON-如何将纯格式转换为JSON格式?

[英]JSON - how to convert plain format into json format?

I got the response after invoke from worklight adapter. 从worklight适配器调用后,我得到了响应。

{"text": "{\\n \\"responseCode\\" : \\"00\\",\\n \\"responseMsg\\" : null,\\n \\"buildFromAccountsMap\\" : {\\n \\"1000071000005844 D\\" : \\"1000071000005844\\",\\n \\"1000791000030636 D\\" : \\"1000791000030636\\",\\n \\"1001911000036935 D\\" : \\"1001911000036935\\",\\n \\"1002021000029411 D\\" : \\"1002021000029411\\",\\n \\"1005071000029666 D\\" : \\"1005071000029666\\",\\n \\"1005071000033139 D\\" : \\"1005071000033139\\",\\n \\"1005071000037533 D\\" : \\"1005071000037533\\",\\n \\"1005071000038605 D\\" : \\"1005071000038605\\",\\n \\"1005071000045298 D\\" : \\"1005071000045298\\",\\n \\"1005071000045517 D\\" : \\"1005071000045517\\",\\n \\"1005071000046989 D\\" : \\"1005071000046989\\",\\n \\"1005071000056183 D\\" : \\"1005071000056183\\",\\n \\"1005491000019560 D\\" : \\"1005491000019560\\",\\n \\"2000071000163308 S\\" : \\"2000071000163308\\",\\n \\"2000071000163361 S\\" : \\"2000071000163361\\"\\n }} {“ text”:“ {\\ n \\” responseCode \\“:\\” 00 \\“,\\ n \\” responseMsg \\“:空,\\ n \\” buildFromAccountsMap \\“:{\\ n \\” 1000071000005844 D \\“:\\ “ 1000071000005844 \\”,\\ n \\“ 1000791000030636 D \\”:\\“ 1000791000030636 \\”,\\ n \\“ 1001911000036935 D \\”:\\“ 1001911000036935 \\”,\\ n \\“ 1002021000029411 D \\”:\\“ 1002021000029411 \\”, \\ n \\“ 1005071000029666 D \\”:\\“ 1005071000029666 \\”,\\ n \\“ 1005071000033139 D \\”:\\“ 1005071000033139 \\”,\\ n \\“ 1005071000037533 D \\”:\\“ 1005071000037533 \\”,\\ n \\“ 1005071000038605 D \\“:\\” 1005071000038605 \\“,\\ n \\” 1005071000045298 D \\“:\\” 1005071000045298 \\“,\\ n \\” 1005071000045517 D \\“:\\” 1005071000045517 \\“,\\ n \\” 1005071000046989 D \\“:\\ “” 1005071000046989 \\“,\\ n \\” 1005071000056183 D \\“:\\” 1005071000056183 \\“,\\ n \\” 1005491000019560 D \\“:\\” 1005491000019560 \\“,\\ n \\” 2000071000163308 S \\“:\\” 2000071000163308 \\“, \\ n \\“ 2000071000163361 S \\”:\\“ 2000071000163361 \\” \\ n}}

My worklight adapter 我的工作灯适配器

function buildFromAccounts(userId) {
    path = "xxxxxxxxxxxxxxxxx";

    var input = {
        method : 'post',
        returnedContentType : 'plain',
        path : path,
        body:{
            contentType:'application/json; charset=UTF-8',
            content:
                JSON.stringify({
                    "userId": userId.toString()

                })
            }
    };


    return WL.Server.invokeHttp(input);
}

The problem is, how I can convert this plain format into json format in worklight? 问题是,如何在Worklight中将这种纯格式转换为json格式?

What did you set for returnedContentType ? 您为returnedContentType设置了什么? JSON or plain? JSON还是纯文本?

Edit: since you are returning plain... 编辑:由于您返回平原...
Try something akin to the following: var obj = JSON.parse(response.text) 尝试类似以下操作: var obj = JSON.parse(response.text)

Looks like your backend returns json. 看起来您的后端返回json。 no reason to treat it like a plaintext. 没有理由将其视为纯文本。 change 更改

returnedContentType : 'plain' 

to

returnedContentType : 'json' 

(optionally - omit this property at all, WL server will try to detect response type automatically) (可选-完全忽略此属性,WL服务器将尝试自动检测响应类型)

Option #2 - you can always use 选项#2-您可以随时使用

var jsonObj = JSON.parse(jsonString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM