5

我对json有问题。我希望我的 Json 看起来像:

 data={"phoneId":1,"token":"APA91bF2tN5g1TtULFE5tysRMAarygjX4w9hjTGCqT3SL-PwiMV6aqTtkV3lpqLkc7msVfEdTnyd_pJVFNMM_fjEbeVSuCjiNPVKx7p9sYC1DoWnuKUurt31E1yh2RDwl_oprfKxEF18PP6Q8dXHZe6FeflE3CIxBg","appId":5}

这是我发给网络服务的帖子:

JSONObject jsonObject = new JSONObject();
                     jsonObject.put("token", regId);
                     jsonObject.put("appId", GlobalConfig.getAPPLICATION_ID());
                     jsonObject.put("phoneId", 1);

                     JSONArray jArrayParam = new JSONArray();
                     jArrayParam.put(jsonObject);

                     JSONObject finaljsonobj = new JSONObject();

                     finaljsonobj.put("data", jArrayParam);

                System.out.println(jsonObject.toString());
                List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
                nameValuePair.add(new BasicNameValuePair("Token",jArrayParam.toString()));

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(GlobalConfig.getSendEmail());
                  httppost.addHeader("Authorization", "Basic " + Base64.encodeToString(
                            (GlobalConfig.getAuthString()).getBytes(),Base64.NO_WRAP));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

如何检查我的 json 在我的示例中是否看起来像一个?我想检查我的 json 是如何发布到 web 服务的。

4

3 回答 3

2

尝试:

JSONObject jsonObject = new JSONObject();
jsonObject.put("token", regId);
jsonObject.put("appId", GlobalConfig.getAPPLICATION_ID());
jsonObject.put("phoneId", 1);

System.out.println("data="+jsonObject.toString());
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("Token","data="+jsonObject.toString()));

这会将您的 JSON 对象创建为:

data={
  "phoneId": 1,
  "token": "token",
  "appId": 5
}
于 2012-12-19T08:25:06.913 回答
1

你可以为你的 JSONObject 创建包装类:

public class YourWrapperClassName{
    @SerializedName("phoneId")
    private int phoneId;
    @SerializedName("token")
    private String token;
    @SerializedName("appId")
    private int appId;
}

准备您的 JSON 对象并在调试中查看它的外观:

YourWrapperClassName testObj = new YourWrapperClassName();
// initialize testObj fields

Gson gson = new Gson();  // import com.google.gson.Gson; 
String result = gson.toJson(testObj); // put breakpoint here

下载 Gson 库的链接 - http://code.google.com/p/google-gson/downloads/list

于 2012-12-19T08:23:37.610 回答
0

我认为您的 finaljsonobj 具有以下结构:

{"data":[{"phoneId":1,"token":"APA91bF2tN5g1TtULFE5tysRMAarygjX4w9hjTGCqT3SL-PwiMV6aqTtkV3lpqLkc7msVfEdTnyd_pJVFNMM_fjEbeVSuCjiNPVKx7p9sYC1DoWnuKUurt31E1yh2RDwl_oprfKxEF18PP6Q8dXHZe6FeflE3CIxBg","appId":5}]}

问题是 JSONArray,如果您希望具有以下结构:

{"data":{"phoneId":1,"token":"APA91bF2tN5g1TtULFE5tysRMAarygjX4w9hjTGCqT3SL-PwiMV6aqTtkV3lpqLkc7msVfEdTnyd_pJVFNMM_fjEbeVSuCjiNPVKx7p9sYC1DoWnuKUurt31E1yh2RDwl_oprfKxEF18PP6Q8dXHZe6FeflE3CIxBg","appId":5}}

只需删除 jArrayParam 并执行以下操作:

finaljsonobj.put("data", jsonObject);
于 2012-12-19T08:20:11.730 回答