生成JSON数组

import org.json.JSONArray;
import org.json.JSONObject;


JSONObject ingredients = new JSONObject();
ingredients.put("apples", "3kg");
ingredients.put("sugar", "1kg");
ingredients.put("pastry", "2.4kg");
ingredients.put("bestEaten", "outdoors");
    	
JSONObject ingredients2 = new JSONObject();
ingredients2.put("apples", "5kg");
ingredients2.put("sugar", "2kg");
ingredients2.put("pastry", "3.8kg");
ingredients2.put("bestEaten", "indoors");
    	
JSONArray jsonArray = new JSONArray();
jsonArray.put(ingredients);
jsonArray.put(ingredients2);

提示:JSONArray和JSONObject的包路径需要一致

你可能感兴趣的:(JAVA)