java Json String - 如何在字符串中添加变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42951087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Json String - How to add variables in the string
提问by user2967948
I have a Json string like below
我有一个像下面这样的 Json 字符串
String jsonRequestString = "{\"access_code\" : \"9bPbN3\" , "
+ "\"merchant_reference\" : \"123\", \"language\" : \"en\",\"id\" : \"149018273\","
+ "\"merchant_identifier\" : \"gKc\", \"signature\" : \"570fd712af47995468550bec2655d9e23cdb451d\", "
+ "\"command\" : \"VOID\"}";
I have a String variable as
我有一个字符串变量
String code = "9bPbN3";
Question, how do I plugin the above string instead of hardcoding it at the below place. i.e. instead of 9bPbN3, I want to use the variable code there.
问题,我如何插入上面的字符串而不是在下面的地方对其进行硬编码。即而不是 9bPbN3,我想在那里使用可变代码。
String jsonRequestString = "{\"access_code\" : \"9bPbN3\" , "
Many Thanks in advance.
提前谢谢了。
回答by Suresh Atta
If you are struggling to arrange the "
's the correct syntax would be
如果您正在努力安排"
's 正确的语法是
String jsonRequestString = "{\"access_code\" : \""+code+"\" , ";
Instead of formatting Json string manually, which takes alot of effort, consider using a library or util.
与其手动格式化 Json 字符串,这需要很多努力,不如考虑使用库或实用程序。
For ex (going to use Hymansonlibrary) :
例如(将使用Hymanson库):
Request re = new Request();
re.setCode(code);
...
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(re);
回答by Ollie in PGH
String yourVariable = "xyz";
String jsonRequestString = "{\"access_code\" : \"" + yourVariable + "\" , "
+ "\"merchant_reference\" : \"123\", \"language\" : \"en\",\"id\" : \"149018273\","
+ "\"merchant_identifier\" : \"gKc\", \"signature\" : \"570fd712af47995468550bec2655d9e23cdb451d\", "
+ "\"command\" : \"VOID\"}";
回答by tweber
General advice is to avoid crafting a json structure out of vanilla strings. Instead use a json parser/writer library for this operations.
一般建议是避免使用普通字符串制作 json 结构。而是使用 json 解析器/编写器库进行此操作。
Checkout http://stleary.github.io/JSON-java/index.html/ http://stleary.github.io/JSON-java/index.html.
结帐http://stleary.github.io/JSON-java/index.html/ http://stleary.github.io/JSON-java/index.html。
There a various other libraries and tutorials available.
还有各种其他库和教程可用。
If you don't want to go this direction, use a "known value" placeholder and substitute it. So the full json would contain "access_code" : "@@ACCESS_CODE@@" and you would Substitute the placeholder with the real value. So your json string would be some kind of a string template.
如果您不想朝这个方向发展,请使用“已知值”占位符并替换它。因此,完整的 json 将包含 "access_code" : "@@ACCESS_CODE@@" 并且您将用实际值替换占位符。所以你的 json 字符串将是某种字符串模板。
回答by Jerfov2
Another option would be to use the format
method like so:
另一种选择是使用如下format
方法:
String jsonRequestString = "{\"access_code\" : \"%s\" , "
+ "\"merchant_reference\" : \"123\", \"language\" : \"en\",\"id\" : \"149018273\","
+ "\"merchant_identifier\" : \"gKc\", \"signature\" : \"570fd712af47995468550bec2655d9e23cdb451d\", "
+ "\"command\" : \"VOID\"}";
String code = "9bPbN3";
String result = String.format(jsonRequestString, code);
Notice the "%s" I put in the place of where code
would go. When you call the format
method with code
as a parameter, it puts it where the "%s" was.
注意“%s”我放在 where code
would go的地方。当您将format
方法code
作为参数调用时,它会将它放在“%s”所在的位置。