用于将JSON转换为urlencoded的库 [英] Library to convert JSON to urlencoded

查看:584
本文介绍了用于将JSON转换为urlencoded的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在对一个非常不一致的(Zurmo-)REST API进行一些集成。 API仅接受urlencoded字符串作为http帖子中的有效负载,但它以JSON回答。
因为文档对此非常不清楚,我们自然认为我们可以发布JSON,但事实并非如此。

We are doing some integration towards a quite inconsistent (Zurmo-)REST API. The API only accepts urlencoded strings as its payload in the http posts, but it answers with JSON. So as the documentation was very unclear on this we naturally thought we could post JSON to it, but this was not the case.

所以现在我们全部当我们需要将它发送为x-www-form-urlencoded时,我们的代码生成JSON,是否有任何 java 库可以从JSON转换为urlencoded字符串?

So now we have all our code generating JSON when we need to send it as x-www-form-urlencoded, is there any java library that can do a conversion from JSON to an urlencoded string?

我们目前正在使用org.json lib,但如果需要,我们可以更改它。

We are currently using the org.json lib, but we can change it if there would be a need for it.

示例:

此JSON字符串:

{"data":{"description":"test","occurredOnDateTime":"2013-10-24 01:44:50"}}

应转换为:

data%5Bdescription%5D=test&data%5BoccurredOnDateTime%5D=2013-10-24+01%3A44%3A50

Java代码:

我们将rasmushaglunds javascript代码翻译成java并将其包装好,如果有其他人的话,这里就是结果了请问这个问题。

We translated rasmushaglunds javascript code to java and wrapped it, here is the result if anybody else stumbles upon this problem.

public static String jsonToURLEncoding(JSONObject json) {
    String output = "";
    String[] keys = JSONObject.getNames(json);
    for (String currKey : keys)
        output += jsonToURLEncodingAux(json.get(currKey), currKey);

    return output.substring(0, output.length()-1);
}

private static String jsonToURLEncodingAux(Object json, String prefix) {
    String output = "";
    if (json instanceof JSONObject) {
        JSONObject obj = (JSONObject)json;
        String[] keys = JSONObject.getNames(obj);
        for (String currKey : keys) {
            String subPrefix = prefix + "[" + currKey + "]";
            output += jsonToURLEncodingAux(obj.get(currKey), subPrefix);
        }
    } else if (json instanceof JSONArray) {
        JSONArray jsonArr = (JSONArray) json;
        int arrLen = jsonArr.length();

        for (int i = 0; i < arrLen; i++) {
            String subPrefix = prefix + "[" + i + "]";
            Object child = jsonArr.get(i);
            output += jsonToURLEncodingAux(child, subPrefix);
        }
    } else {
        output = prefix + "=" + json.toString() + "&";
    }

    return output;
}


推荐答案

如下所述,它不是一个Java库但你应该能够翻译它:)

As noted below, it's not a Java library but you should be able to translate it :)

以下是你在javascript中的表现:

Here's how you could do it in javascript:

var jsonArrayToUrl = function (obj, prefix) {
  var urlString = "";
  for (var key in obj) {
    if (obj[key] !== null && typeof obj[key] == "object") {
      prefix += "[" + key + "]";
      urlString += jsonArrayToUrl(obj[key], prefix);
    }else{
      urlString += prefix + "[" + key + "]=" + obj[key] + "&";
    }
  }
  return encodeURIComponent(urlString);
};

然后拨打

jsonArrayToUrl(test["data"], "data");

通过上面给出的示例字符串返回

By the example string you gave above it returns

"data%5Bdescription%5D%3Dtest%26data%5BoccurredOnDateTime%5D%3D2013-10-24%2001%3A44%3A50%26"

它应该在嵌套数组上递归工作。您也可以考虑为函数编写一个包装器,这样您只需要一个参数。

It should work recursively on nested arrays. You might also consider writing a wrapper for the function so that you only need one argument.

这篇关于用于将JSON转换为urlencoded的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆