字符串拼接

来源:互联网 发布:b2b网络平台有哪些 编辑:程序博客网 时间:2024/04/25 23:03

发送给服务器接口的字符串,往往需要字符拼接

//字符串拼接private String dataAppend(){JSONObject ClientKey=new JSONObject();try {ClientKey.put("hostPhoneNumber", getLoginName());ClientKey.put("method", "callRecords");}catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}/* 把JSON数据转换成String类型使用输出流向服务器写 */String content=String.valueOf(ClientKey);return content;}

<span style="font-size:18px">public String petToJson(Pet pet) {          String jsonresult = "";//定义返回字符串          JSONObject object = new JSONObject();//创建一个总的对象,这个对象对整个json串          try {              JSONArray jsonarray = new JSONArray();//json数组,里面包含的内容为pet的所有对象              JSONObject jsonObj = new JSONObject();//pet对象,json形式              jsonObj.put("petid", pet.getPetid());//向pet对象里面添加值              jsonObj.put("petname", pet.getPetname());              jsonObj.put("pettype", pet.getPettype());              // 把每个数据当作一对象添加到数组里              jsonarray.put(jsonObj);//向json数组里面添加pet对象              object.put("pet", jsonarray);//向总对象里面添加包含pet的数组                          jsonresult = object.toString();//生成返回字符串          } catch (JSONException e) {              // TODO Auto-generated catch block              e.printStackTrace();          }          LogI("生成的json串为:"+jsonresult);          return jsonresult;      }</span>  


最后生成结果为:{"pet":[{"petid":100,"petname":"name1","pettype":"type1"}]}


//字符串拼接 private String dataAppend(){ JSONObject ClientKey=new JSONObject();  try {  ClientKey.put("method", "orderConference"); ClientKey.put("meetingTopicName", orderMeetingTopic); ClientKey.put("hostNumber", orderHostNum); JSONArray jsonarray = new JSONArray();//json数组,里面包含的内容为pet的所有对象  for (Iterator<String> it = CompanyListActivity.map.keySet().iterator(); it.hasNext();) {    JSONObject jsonObj = new JSONObject();//pet对象,json形式 String key = it.next();LocalMailList data = CompanyListActivity.map.get(key);                 jsonObj.put("name", data.getUserName());//向name对象里面添加值           if (SysUtils.isMobileNO(data.getUserNum())) {jsonObj.put("phoneNumber", CodeConst.CODE_PHONE+data.getUserNum());} else {jsonObj.put("phoneNumber", data.getUserNum());}          jsonarray.add(jsonObj);//向json数组里面添加pet对象            }                    System.out.println(" ====jsonarray"+ jsonarray.toJSONString()); ClientKey.put("attendee",jsonarray);//向总对象里面添加包含pet的数组  }catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }  /* 把JSON数据转换成String类型使用输出流向服务器写 */ String content=String.valueOf(ClientKey);    System.out.println("生成的json串为:"+content);     return content;  }


最后生成的结果:

{"attendee":[{"name":"zz","phoneNumber":"12344"},{"name":"zzz","phoneNumber":"1223"},{"name":"zzz","phoneNumber":"1233"}],

"hostNumber":"123456768",

"meetingTopicName":"你就",

"method":"orderConference"}

0 0