简体   繁体   中英

JSON - how to convert plain format into json format?

I got the response after invoke from worklight adapter.

{"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 }}

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?

What did you set for returnedContentType ? JSON or plain?

Edit: since you are returning plain...
Try something akin to the following: var obj = JSON.parse(response.text)

Looks like your backend returns 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)

Option #2 - you can always use

var jsonObj = JSON.parse(jsonString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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