字符串拼接

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

	//字符串拼接

	private String dataAppend(){
		JSONObject ClientKey=new JSONObject();
		try {
			
			ClientKey.put("hostPhoneNumber", getLoginName());
			ClientKey.put("method", "callRecords");
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值