com.jjtree.utilities.JConverter.java Source code

Java tutorial

Introduction

Here is the source code for com.jjtree.utilities.JConverter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jjtree.utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author rose
 */
public class JConverter {

    public static JSONObject convert(HttpServletRequest request) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = request.getReader();
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } finally {
            reader.close();
        }

        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(sb.toString());
        } catch (JSONException ex) {
            Logger.getLogger(JConverter.class.getName()).log(Level.SEVERE, null, ex);
        }

        return jsonObject;
    }

}