Java tutorial
/* * This file is part of Minus-Java. * * Minus-Java is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Minus-Java is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Minus-Java. If not, see <http://www.gnu.org/licenses/>. */ package net.paissad.minus.utils; import java.util.HashMap; import java.util.Map; import org.json.simple.JSONObject; import org.json.simple.JSONValue; import org.json.simple.parser.ParseException; import net.paissad.minus.exception.MinusException; /** * @author Papa Issa DIAKHATE (paissad) * */ public class JSONUtils { /** * Parse a String (usually an HTTP response which is a JSON formatted * String) and returns its <tt>Map</tt> view. * * @param response - A formatted JSON <tt>String</tt>. * @return A {@link Map} representing the JSON response. * @throws ParseException * @throws MinusException If the specified <tt>String</tt> cannot be parsed * as a <tt>Map</tt> or is not a <tt>JSONObject</tt>. */ public static Map<String, Object> getMapFromJSONString(final String response) throws ParseException, MinusException { Object obj = JSONValue.parseWithException(response); if (obj instanceof JSONObject) { @SuppressWarnings("unchecked") Map<String, Object> jsonResult = ((HashMap<String, Object>) obj); return jsonResult; } else { throw new MinusException("The JSON string is not of type JSONObject => " + response); } } }