android 混合型json格式文件生成 代码

android 创建复杂json格式文件代码思路。在设计json数据hashmap数据的时候添加标记位。如生成单个json数据,则标记位位single,如需json数组长度大于1 则标记位位multi.

并且次吃的json数组名相同。

/**
*  创建混合json数据代码
* @param mList
* @return
* @throws JSONException
*/
public static JSONObject createJsonDate(List<HashMap> mList)
throws JSONException {
// 声明json对象
if (mList.isEmpty()) {
Log.d(AutoReportUtil.TAG, "mList is empty");
return null;
}
JSONArray jsonArray2 = new JSONArray();
String jsonName = "";
String jsonNameMuiti = "";
JSONObject outData = new JSONObject();
for (int i = 0; i < mList.size(); i++) {    
HashMap<String, String> map = mList.get(i);
if (map != null) {
if (map.get("sign").equals("single")) {   //单独的数组
map.remove("sign");
JSONArray jsonArray = new JSONArray();
JSONObject node = new JSONObject();
Iterator<?> iter = map.entrySet().iterator();
while (iter.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) iter.next();
if (!entry.getKey().equals("arrayName")) {
node.put((String) entry.getKey(), entry.getValue());
} else {
jsonName = (String) entry.getValue();
}
}
jsonArray.put(node);
outData.put(jsonName, jsonArray);
} else if (map.get("sign").equals("muiti")) { //复杂的数据 
JSONObject node = new JSONObject();
Iterator<?> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
if (entry.getKey().equals("arrayName")) {//过滤数组名
jsonNameMuiti = (String) entry.getValue();
} else if (entry.getKey().equals("sign")) { //过滤标签 
//
} else {
node.put((String) entry.getKey(), entry.getValue());
}
}
jsonArray2.put(node);
}


}


}
if (jsonArray2.length() != 0) {
outData.put(jsonNameMuiti, jsonArray2);
}
return outData;
}



//需要生成的json格式如下

{
"test_report_1":[
{"test_2":"222222","test_6":"666666","test_5":"555555","test_4":"444444","test_1":"111111","test_3":"333333"}],
"test_report_2":[
{"reportDate2_4":"444444","reportDate2_6":"666666","reportDate2_5":"555555","reportDate2_3":"333333","reportDate2_2":"222222","reportDate2_1":"111111"}],
"test_result":[
{"case_name":"测试案例1","case_end_time":"2015-07-03 15-15-25","case_start_time":"2015-07-03 15-15-15","case_index":"0"},
{"case_name":"测试案例2","case_end_time":"2015-07-03 15-15-25","case_start_time":"2015-07-03 15-15-15","case_index":"1"},
{"case_name":"测试案例3","case_end_time":"2015-07-03 15-15-25","case_start_time":"2015-07-03 15-15-15","case_index":"0"},
{"case_name":"测试案例4","case_end_time":"2015-07-03 15-15-25","case_start_time":"2015-07-03 15-15-15","case_index":"0"}
]
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值