3

我正在尝试通过 HTTP POST 参数发送一个 JSON 对象,其中包含一个带有 JSON 对象的 JSON 数组。

参数的格式(服务器期望的)类似于:

{""team"":[
    {""teamid"":""179228"",""position"":1},
    {""teamid"":""218036"",""position"":2},
    {""teamid"":""88109"",""position"":3},
    {""teamid"":""88111"",""position"":4},
    {""teamid"":""165536"",""position"":5},
    {""teamid"":""224645"",""position"":6}
]}

然而,发送的是:

{"team":"[
\"{\\\"position\\\":0,\\\"teamid\\\":\\\"88107\\\"}\",\"{\\\"position\\\":1,\\\"teamid\\\":\\\"88109\\\"}\",\"{\\\"position\\\":2,\\\"teamid\\\":\\\"156714\\\"}\",\"{\\\"position\\\":3,\\\"teamid\\\":\\\"138877\\\"}\",\"{\\\"position\\\":4,\\\"teamid\\\":\\\"168730\\\"}\",\"{\\\"position\\\":5,\\\"teamid\\\":\\\"88110\\\"}\",\"{\\\"position\\\":6,\\\"teamid\\\":\\\"88111\\\"}\",\"{\\\"position\\\":7,\\\"teamid\\\":\\\"134431\\\"}\",\"{\\\"position\\\":8,\\\"teamid\\\":\\\"88112\\\"}\",\"{\\\"position\\\":9,\\\"teamid\\\":\\\"138507\\\"}\",\"{\\\"position\\\":10,\\\"teamid\\\":\\\"138880\\\"}\",\"{\\\"position\\\":11,\\\"teamid\\\":\\\"138881\\\"}\",\"{\\\"position\\\":12,\\\"teamid\\\":\\\"151465\\\"}\",\"{\\\"position\\\":13,\\\"teamid\\\":\\\"151464\\\"}\
"]"}

我构建该 JSON 对象的方式如下:

            JSONArray teamArray = new JSONArray();
            JSONObject jsonRoot = new JSONObject();
            for (int i = 0; i < mTeams.size(); i++) {
                String teamId = null;
                BaseModel data = mTeams.get(i);
                if (data != null && data instanceof TeamModel) {
                    teamId = ((TeamModel) data).getId();
                }
                JSONObject teamObject = new JSONObject();
                try {
                    teamObject.put(
                            getResources().getString(
                                    R.string.sendResortedTeamsPosition), i);
                    teamObject.put(
                            getResources().getString(
                                    R.string.sendResortedTeamsTeamId), teamId);
                    teamArray.put(teamObject);
                } catch (NotFoundException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            try {
                jsonRoot.put("team", teamArray);
                mNameValuePairs.put("teams", jsonRoot);
                    } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

在最后一行 ( jsonRoot.put("team", teamArray);) 中,它的格式与在最后一行中发送的格式相同,但少了\1 次,因此显然“解析”的次数少了 1 次。

我的 HTTP 代码的一部分:

String postBody = json.toString();
Log.d("HTTPHelper", "posting JSON: " + postBody);
((HttpPost) httpRequest).setEntity(new StringEntity(postBody));

为什么会这样?是Java吗?任何想法如何构建正确的 JSON?或任何解决方法?

提前非常感谢!

4

3 回答 3

1

我放弃了这个,并决定采用肮脏的方式:通过以下方式手动替换字符:

json = new JSONObject(nameValuePairs.toString().replace("\"", "'"));
json = new JSONObject(json.toString().replace("\"", ""));

它很丑陋,可能很危险或有风险,但它确实有效......

于 2013-07-08T08:20:45.430 回答
1

此任务的代码太多,请查看此库https://github.com/kodart/Httpzoid 它在内部使用 GSON 并提供与对象一起使用的 API。所有 JSON 详细信息均已隐藏。

Http http = HttpFactory.create(context);
http.get("http://example.com/users")
    .handler(new ResponseHandler<User[]>() {
        @Override
        public void success(User[] users, HttpResponse response) {
        }
    }).execute();
于 2013-07-14T12:59:48.697 回答
0

只是一个提示......我并不是真的喜欢android,只是java ..但你可以在两边使用Base64对你的json进行解码和编码,并传递编码的“字符串”。

所以你不必担心任何“肮脏的方式”更换。

希望这对你或其他人也有帮助。:)

于 2015-05-27T12:56:51.083 回答