请问关于 json ,put 数据的问题

langkejianxin1999 2015-03-16 12:05:26
请问 obj.put("count", count); 当要传如数组的时候,应该怎么写呢?
麻烦会的朋友回复下,谢谢啦

public class AboutActivity extends Activity {
。。。。。
private int count[] = new int[3];
count[0] =1;
count [1]=2;
count [2]=3;

httpapi.update (string, count);
。。。
}


public class HttpApi {
。。。。。
public boolean update(String access_token, int count[] ) {
try {
String target = "http://nihao.ywen.com/api.php";
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(target );
JSONObject obj= new JSONObject();
obj.put("action", "update");
obj.put("access_token", access_token);
if( count != null){

obj.put("count", count); // 程序要调用 连接服务器接口,但是count 是一个数组,但是就这样 count 传进去的只是数组的地址,而是要当打印这个的时候 Log.d(TAG,": obj.toString()" +obj.toString()); 会显示 "count":["1","2","3"]

}

request.setEntity(new StringEntity(obj.toString(),"utf-8"));
//obj.toString()
Log.d(TAG,": obj.toString()" +obj.toString());

HttpResponse response;
response = httpclient.execute(request);
int code = response.getStatusLine().getStatusCode();

if( code==200) {
String rev =EntityUtils.toString(response.getEntity()); //id
JSONObject obj1= new JSONObject(rev);
get_errcode = obj1.getInt("errcode");
get_errmsg = obj1.getString("errmsg");
Log.d(TAG,"yong: register():get_errcode="+get_errcode);
Log.d(TAG,"yong: register():get_errmsg="+get_errmsg);
}
}

}
...全文
585 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
assky124 2015-03-18
  • 打赏
  • 举报
回复
可以试试 Gson 简单而且效率更高
jiang920627 2015-03-17
  • 打赏
  • 举报
回复
引用 7 楼 langkejianxin1999 的回复:
[quote=引用 6 楼 jiang920627 的回复:] try { JSONObject object = new JSONObject(JSON); String action = (String) object.get("action"); int count1 = (Integer) object.get("count1"); int count12 = (Integer) object.get("count12"); String access_token = (String) object.get("access_token"); Log.e("JSON", action+"---"+count1+"---"+count12+"---"+access_token); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
@ jiang920627 谢谢您的回复,但是你回复的不太对哦 我的意思是 obj.put("count", count); 当 count 为数组的时候,应该怎么写呢[/quote] 你是解析json ? 还是要生成json ?
jiang920627 2015-03-17
  • 打赏
  • 举报
回复
引用 7 楼 langkejianxin1999 的回复:
[quote=引用 6 楼 jiang920627 的回复:] try { JSONObject object = new JSONObject(JSON); String action = (String) object.get("action"); int count1 = (Integer) object.get("count1"); int count12 = (Integer) object.get("count12"); String access_token = (String) object.get("access_token"); Log.e("JSON", action+"---"+count1+"---"+count12+"---"+access_token); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
@ jiang920627 谢谢您的回复,但是你回复的不太对哦 我的意思是 obj.put("count", count); 当 count 为数组的时候,应该怎么写呢[/quote] try { JSONObject jsonObject = new JSONObject(json); JSONArray jsonArray = jsonObject.getJSONArray("count"); for (int i = 0; i < jsonArray.length(); i++) { Log.e("json", jsonArray.get(i)+""); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
  • 打赏
  • 举报
回复
引用 9 楼 langkejianxin1999 的回复:
[quote=引用 8 楼 u012301841 的回复:] 转成接送对象在传过去 JSONArray jsonArray = JSONArray.fromObject(count);
@u012301841 谢谢您的回复 count[] 这个是 个数字参数,按照上面所写后,会出现错误提示的: The method fromobject [int] is underfined for the type jsonarray JSONArray jsonArray = JSONArray.fromObject(count); request.setEntity(new StringEntity(obj.toString(),"utf-8"));[/quote] 你没有导入gson的官方jar包吧,提示没有这个方法.
langkejianxin1999 2015-03-17
  • 打赏
  • 举报
回复
引用 8 楼 u012301841 的回复:
转成接送对象在传过去 JSONArray jsonArray = JSONArray.fromObject(count);
@u012301841 谢谢您的回复 count[] 这个是 个数字参数,按照上面所写后,会出现错误提示的: The method fromobject [int] is underfined for the type jsonarray JSONArray jsonArray = JSONArray.fromObject(count); request.setEntity(new StringEntity(obj.toString(),"utf-8"));
浅笑_JIE 2015-03-16
  • 打赏
  • 举报
回复
for(int i = 0 ; i < count.length ; i ++){ obj.put("count"+i, count[i]); }
  • 打赏
  • 举报
回复
转成接送对象在传过去 JSONArray jsonArray = JSONArray.fromObject(count);
langkejianxin1999 2015-03-16
  • 打赏
  • 举报
回复
引用 6 楼 jiang920627 的回复:
try { JSONObject object = new JSONObject(JSON); String action = (String) object.get("action"); int count1 = (Integer) object.get("count1"); int count12 = (Integer) object.get("count12"); String access_token = (String) object.get("access_token"); Log.e("JSON", action+"---"+count1+"---"+count12+"---"+access_token); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
@ jiang920627 谢谢您的回复,但是你回复的不太对哦 我的意思是 obj.put("count", count); 当 count 为数组的时候,应该怎么写呢
jiang920627 2015-03-16
  • 打赏
  • 举报
回复
try {
JSONObject object = new JSONObject(JSON);
String action = (String) object.get("action");
int count1 = (Integer) object.get("count1");
int count12 = (Integer) object.get("count12");
String access_token = (String) object.get("access_token");
Log.e("JSON", action+"---"+count1+"---"+count12+"---"+access_token);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
langkejianxin1999 2015-03-16
  • 打赏
  • 举报
回复
麻烦会的朋友帮忙回复下吧,谢啦
langkejianxin1999 2015-03-16
  • 打赏
  • 举报
回复
引用 2 楼 assky124 的回复:
JSONArray 试过了么?
@assky124 请问怎么写呢? 麻烦详细回复下吧 谢谢啦
langkejianxin1999 2015-03-16
  • 打赏
  • 举报
回复
引用 1 楼 u011070145 的回复:
for(int i = 0 ; i < count.length ; i ++){ obj.put("count"+i, count[i]); }
@ u011070145 谢谢回复 这种方法不行哦 得到的log是: obj.toString(){"count1":2,"action":"user_info_update","count12":3,"access_token":"5dab374ab87bc0506934ef5afd43a61a","count1":1} 麻烦再回复下吧 谢啦
assky124 2015-03-16
  • 打赏
  • 举报
回复
JSONArray 试过了么?

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧