Android版的JSONObject:阵列添加到put方法 [英] Android JSONObject : add Array to the put method

查看:391
本文介绍了Android版的JSONObject:阵列添加到put方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  // JSON对象来保存信息,该信息发送给服务器
JSONObject的jsonObjSend =新的JSONObject();
jsonObjSend.put(行动,myAction);
jsonObjSend.put(类型,TIPO);

有关现在一切OK,但如果我想补充

  jsonObjSend.put(ELEMENTI,arrayOfElements);

在这里arrayOf元素必须是一个字符串数组。我该怎么办?

/ ** 修改

例如 - 我需要什么

  {
  行动:myAction
  类型:ELEMENTI,
  ELEMENTI:[
    3287498357,
    23472857
  ]
}


解决方案

看到我明白你正在尝试做类似的事情作为询问的 Java的JSONObject的数组值键入

  jsonObjSend.put(ELEMENTI,新的JSONArray(新的对象[] {值1,值2,值3}));

要简化:

  JSONArray ARR =新JSONArray();
arr.put(值1);
arr.put(值2);
// ...
jsonObjSend.put(ELEMENTI,ARR);

// JSON object to hold the information, which is sent to the server
JSONObject jsonObjSend = new JSONObject();
jsonObjSend.put("action", "myAction");
jsonObjSend.put("type", tipo);

For now is everything ok but if I want to add

jsonObjSend.put("elementi", arrayOfElements);

where arrayOf Elements must be an array of strings. How can I do?

/** EDIT

EXAMPLE OF WHAT I NEED

{
  "action": "myAction",
  "type": "elementi",
  "elementi": [
    "3287498357",
    "23472857"
  ]
}

解决方案

After seeing the example I understood that you are trying to do something similar as asked in Java JsonObject array value to key

jsonObjSend.put("elementi", new JSONArray(new Object[] { "value1", "value2", "value3"} ));

To simplify:

JSONArray arr = new JSONArray();
arr.put("value1");
arr.put("value2");
//...
jsonObjSend.put("elementi", arr);

这篇关于Android版的JSONObject:阵列添加到put方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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