java 生成jsonarray_29:java-------组装生成json数组,解析json

JSONObject jsonData=JSONObject.fromObject(orderComInfo.get("image_items").toString());

JSONArray machinejson= JSONArray.fromObject(jsonData.get("imageItems").toString());

if(com.get(i).get("image_items")!=null&&!com.get(i).get("image_items").toString().equals(""))

{

JSONObject jsonObj= JSON.parseObject(com.get(i).get("image_items").toString());//JSONObject jsonObj1 = JSON.parseObject(jsonObj.get("imageItems").toString());

JSONArray machinejson= JSONArray.fromObject(jsonObj.get("imageItems").toString());//com.alibaba.fastjson.JSONArray result = jsonObj.getJSONArray(jsonObj.get("imageItems").toString());

com.get(i).put("imageItems", machinejson);

}else if(com.get(i).get("image_items")==null)

{

JSONArray machinejson= newJSONArray();

com.get(i).put("imageItems", machinejson);

}

packagecom.action;importjava.util.HashMap;importjava.util.Map;importjava.util.Map.Entry;importjava.util.Set;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;public classTestDemo {public static voidmain(String[] args) {

JSONObject json= newJSONObject();

JSONObject json1= newJSONObject();//TODO Auto-generated method stub

json1.put("unit_id", "1");

json1.put("title", "单局价格");

json1.put("price", "1");

json1.put("unit", "局/元");

JSONObject json2= newJSONObject();

json1.put("unit_id", "2");

json2.put("title", "时间单价");

json2.put("price", "1");

json2.put("unit", "时/元");

JSONArray jsonArray= newJSONArray();

jsonArray.add(0, json1);

jsonArray.add(1, json2);

json.put("unitItems", jsonArray);

System.out.println(json);

}

}

结果:{"unitItems":[{"unit":"局/元","title":"单局价格","price":"1","unit_id":"2"},{"unit":"时/元","title":"时间单价","price":"1"}]}

package com.util;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

/*

* 用户端改版文档: https://shimo.im/doc/WajjWov1h9M4ffpJ?r=Y97MG/%E3%80%8C%E6%89%93%E7%89%8C%E5%95%A6%E7%94%A8%E6%88%B7%E7%AB%AF%E5%A4%A7%E6%94%B9%E7%89%88%E4%BB%A5%E5%8F%8A...%E5%8F%98%E5%8A%A8%E3%80%8D

*

*/

public class JsonDemo {

public static void main(String[] args) {

JSONObject bigjson = new JSONObject();

JSONObject json = new JSONObject();

//JSONObject json = new JSONObject();

JSONObject json1 = new JSONObject();

// TODO Auto-generated method stub

json1.put("qpsId", "35");

json1.put("qpsDoorImg", "http://image-oss.dapail.com/image/0bitmap_filed672a8d619cc41f19267726e497f1b27.jpg");

json1.put("qpsName", "唐邦测试棋牌室");

json1.put("storeLabel", "互联网机麻");

json1.put("qpsArea", "栖霞区");

json1.put("score", 5.0);

json1.put("payStandardTime", "15");

json1.put("payStandardGame", "1");

json1.put("distance", "<100m");

json1.put("voteNum", "405票");

JSONArray jsoninArray = new JSONArray();

JSONObject jsonin1 = new JSONObject();

jsonin1.put("content", "Wifi");

JSONObject jsonin2 = new JSONObject();

jsonin2.put("content", "空调");

JSONObject jsonin3 = new JSONObject();

jsonin3.put("content", "免费小食");

jsoninArray.add(0, jsonin1);

jsoninArray.add(1, jsonin2);

jsoninArray.add(2, jsonin3);

json1.put("contentItems",jsoninArray);

JSONArray jsonArray = new JSONArray();

jsonArray.add(0, json1);

json.put("qpsItems", jsonArray);

/*json.put("cardTitle", "附近棋牌室");

json.put("moreContent", "更多");*/

bigjson.put("cardname", json);

System.out.println(json);

}

}

JSONObject jo=new JSONObject(unitItems); //创建json对象

JSONArray ja=new JSONArray( jo.get("unitItems").toString());

for (int i = 0; i < ja.length(); i++) {

JSONObject job =new JSONObject(ja.get(i).toString());//遍历

if(job.get("unit_id")!=null&&job.get("unit_id").toString().equals("1"))

{"imageItems":[{"imageUrl":"https://images-cdn.shimo.im/JHeOGXAwftUD8hy9/image.png"},{"imageUrl":"https://images-cdn.shimo.im/JHeOGXAwftUD8hy9/image.png"}]}

JSONObject jsonObj = JSON.parseObject(com.get(i).get("image_items").toString());//JSONObject jsonObj1 = JSON.parseObject(jsonObj.get("imageItems").toString());

JSONArray machinejson= JSONArray.fromObject(jsonObj.get("imageItems").toString());

JSONObject jsonObj = JSON.parseObject(com.get(i).get("image_items").toString());//JSONObject jsonObj1 = JSON.parseObject(jsonObj.get("imageItems").toString());

JSONArray machinejson= JSONArray.fromObject(jsonObj.get("imageItems").toString());

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值