Is there any native function to convert json to url parameters?

后端 未结 11 1904
再見小時候
再見小時候 2020-11-29 22:00

I need convert json object to url form like: \"parameter=12&asd=1\"

I done with this:

        var data = {
            \'action\':\'actualiza_res         


        
11条回答
  •  心在旅途
    2020-11-29 22:34

    Like @georg said, you can use JQuery.param for flat objects.

    If you need to process complex objects, you can use JsonUri, a python package that does just that. There is JavaScript library for it as well

    Disclaimer: I am the author of JSONURI

    Edit: I learned much later that you can also just base64 encode your payload - most languages as support for base64 encoding/decoding

    Example

    x = {name: 'Petter', age: 47, places: ['Mozambique', 'Zimbabwe']}
    stringRep = JSON.stringify(x)
    encoded = window.btoa(stringRep)
    

    Gives you eyJuYW1lIjoiUGV0dGVyIiwiYWdlIjo0NywicGxhY2VzIjpbIk1vemFtYmlxdWUiLCJaaW1iYWJ3ZSJdfQ==, which you can use as a uri parameter

    decoded = window.atob(encoded)
    originalX = JSON.parse(decoded)
    

    Needless to say, it comes with its own caveats

提交回复
热议问题