使用JAVA将QueryString转换为Json

时间:2021-01-18 21:45:50

I want to convert "id=1&location=india" querystring to {"id":"1","location":"india"} json format using java.

我想使用java将“id = 1&location = india”querystring转换为{“id”:“1”,“location”:“india”} json格式。

I am using spring version 4.0.3.

我使用的是Spring 4.0.3版。

3 个解决方案

#1


Well this is not a direct way, but this is the first though came to my mind. Convert the queryString to Hashtable using HttpUtils , then marshall it into JSON.

嗯,这不是直接的方式,但这是第一个虽然我想到了。使用HttpUtils将queryString转换为Hashtable,然后将其编组为JSON。

https://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpUtils.html

Update: HttpUtils produces Hashtable.

更新:HttpUtils生成Hashtable。

#2


I think you can write a converter like this

我想你可以写一个像这样的转换器

public class ConvertToJson {
    public static void main(String[] args) {
        String a = "id=1&location=india";
        System.out.println(convert(a));
    }

    private static String convert(String a) {
        String res = "{\"";

        for (int i = 0; i < a.length(); i++) {
            if (a.charAt(i) == '=') {
                res += "\"" + ":" + "\"";
            } else if (a.charAt(i) == '&') {
                res += "\"" + "," + "\"";
            } else {
                res += a.charAt(i);
            }
        }
        res += "\"" + "}";
        return res;
    }
}

it can do the job for you

它可以为你完成这项工作

#3


try this..

 try
    {
        String query="id=1&location=india";
        String queryArray[]=query.split("&");
        String id[]=queryArray[0].split("=");
        String location[]=queryArray[1].split("=");

        JSONObject jsonObject=new JSONObject();
        jsonObject.put(id[0],id[1]);
        jsonObject.put(location[0],location[1]);

        Log.i("*",jsonObject.toString());
    }
    catch (JSONException e)
    {
        e.printStackTrace();
        Log.i("*",e.getMessage());
    }

#1


Well this is not a direct way, but this is the first though came to my mind. Convert the queryString to Hashtable using HttpUtils , then marshall it into JSON.

嗯,这不是直接的方式,但这是第一个虽然我想到了。使用HttpUtils将queryString转换为Hashtable,然后将其编组为JSON。

https://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpUtils.html

Update: HttpUtils produces Hashtable.

更新:HttpUtils生成Hashtable。

#2


I think you can write a converter like this

我想你可以写一个像这样的转换器

public class ConvertToJson {
    public static void main(String[] args) {
        String a = "id=1&location=india";
        System.out.println(convert(a));
    }

    private static String convert(String a) {
        String res = "{\"";

        for (int i = 0; i < a.length(); i++) {
            if (a.charAt(i) == '=') {
                res += "\"" + ":" + "\"";
            } else if (a.charAt(i) == '&') {
                res += "\"" + "," + "\"";
            } else {
                res += a.charAt(i);
            }
        }
        res += "\"" + "}";
        return res;
    }
}

it can do the job for you

它可以为你完成这项工作

#3


try this..

 try
    {
        String query="id=1&location=india";
        String queryArray[]=query.split("&");
        String id[]=queryArray[0].split("=");
        String location[]=queryArray[1].split("=");

        JSONObject jsonObject=new JSONObject();
        jsonObject.put(id[0],id[1]);
        jsonObject.put(location[0],location[1]);

        Log.i("*",jsonObject.toString());
    }
    catch (JSONException e)
    {
        e.printStackTrace();
        Log.i("*",e.getMessage());
    }