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 = new JSONArray();
                    com.get(i).put("imageItems", machinejson);
                }
                


package com.action;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TestDemo {


    public static void main(String[] args) {
        
        JSONObject json = new JSONObject();

        JSONObject json1 = new JSONObject();
        // TODO Auto-generated method stub
        json1.put("unit_id", "1");
        json1.put("title", "单局价格");
        json1.put("price", "1");
        json1.put("unit", "局/元");

        JSONObject json2 = new JSONObject();
        json1.put("unit_id", "2");
        json2.put("title", "时间单价");
        json2.put("price", "1");
        json2.put("unit", "时/元");

        JSONArray jsonArray = new JSONArray();
        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());
原文地址:https://www.cnblogs.com/coriander/p/7246302.html