[英]Convert a string to json format
I am having a string like我有一个像
request_method=GET, domain=com.tables.com, url=//search.do?table-id=549499, original_url=, remote_host=198.192.0.3, user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36", edgescape="", akamai_bot=""
I cannot change the value as its coming from some configurations.我无法更改该值,因为它来自某些配置。 I want to convert the value to JSON format like below.
我想将值转换为 JSON 格式,如下所示。
request_method : GET, domain : com.tables.com, url : //search.do?table-id=549499, original_url :, remote_host : 198.192.0.3, user_agent : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36", edgescape : "", akamai_bot : ""
Is there any way without manually splitting based on the comma and doing it ?有没有办法不根据逗号手动拆分并执行它?
You have to put all these values in a presenter class.您必须将所有这些值放在演示者类中。
Firstly create a presenter class首先创建一个presenter类
Public class DataPresenter
{
private String requestMethod;
private String domain;
private String url;
public String getRequestMethod()
{
return requestMethod;
}
public String setRequestMethod(String requestMethod)
{
this.requestMethod = requestMethod;
}
................ so on.
}
Now you can create the object of DataPresenter Class and set your data in it.现在您可以创建 DataPresenter 类的对象并在其中设置您的数据。
DataPresenter dataPresenter = new DataPresenter();
dataPresenter.setRequestMethod = 'GET';
dataPresenter.setDomain = 'domain';
dataPresenter.setURL = 'http://url';
Now parse the above object into json string using gson library.现在使用 gson 库将上述对象解析为 json 字符串。
Gson gson = new Gson();
String jsonString = gson.toJson(dataPresenter);
And I think that is what you required.我认为这就是你所需要的。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.