Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;

public class Main {
    static String jsonToQueryString(JSONObject jsonObj) {
        StringBuilder sb = new StringBuilder();

        try {

            Iterator<String> i = (Iterator<String>) jsonObj.keys();
            while (i.hasNext()) {
                String key = (String) i.next();
                Object value;
                try {
                    value = jsonObj.get(key);
                } catch (JSONException e) {
                    value = "";
                }
                sb.append(URLEncoder.encode(key, "UTF-8"));
                sb.append("=");
                sb.append(URLEncoder.encode(value.toString(), "UTF-8"));
                sb.append("&");
            }
        } catch (UnsupportedEncodingException e) {
            System.out.println(e);
        } catch (Exception e) {
        }

        return sb.substring(0, sb.length() - 1);
    }
}