在Java中将URL参数转换为(Jarse)JSON [英] Convert (Parse) URL parameteres to JSON in Java

查看:125
本文介绍了在Java中将URL参数转换为(Jarse)JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有REST客户端,它将URL作为参数提供。但我的云REST基础架构只接受JSON格式。

I have REST client that delivers URL as parameters. But my cloud REST infrastructure accepts only JSON format.

有没有办法在Java中将URL参数转换(解析)为JSON格式?

Is there any way to convert (parse) the URL parameters to JSON format in Java?

网址参数示例:

data=data10&sensor=sensor10&type=type10

到JSON格式如:

{"data":"data10","sensor":"sensor10","type":"type10"}


推荐答案

解决方案



没有开箱即用的解决方案,因此它最多开发一种方法来解决这个问题。

The Solution

There was no solution out of the box, therefore it was most practical to develop a method to solve this.

这是一个参数到JSON的转换器方法:

Here is a Parameters to JSON converter method:

public static String paramJson(String paramIn) {
    paramIn = paramIn.replaceAll("=", "\":\"");
    paramIn = paramIn.replaceAll("&", "\",\"");
    return "{\"" + paramIn + "\"}";
}



结果



方法输入:

Results

Method input:

data=data10&sensor=sensor10&type=type10

方法返回输出:

{"data":"data10","sensor":"sensor10","type":"type10"}

这篇关于在Java中将URL参数转换为(Jarse)JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆