java 将字符串值放入 gson.JsonArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42411136/
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
Put String value in gson.JsonArray
提问by Yasha Jadwani
I want to store a String
in a JsonArray
.
Ex:
我想将 a 存储String
在 a 中JsonArray
。前任:
"virtual_hosts": [ "some_host"]
How should I do this, with the help of java.
我应该如何在 java 的帮助下做到这一点。
JsonArray arr = new JsonArray();
arr.add()
This only lets me add a JsonObject
, but I want to store a String
.
这只能让我添加一个JsonObject
,但我想存储一个String
.
回答by Pascal Schneider
If you are going to use gson by google, looks like you have todo it this way:
如果您打算使用 google 的 gson,看起来您必须这样做:
JsonPrimitive firstHost = new JsonPrimitive("vlbr-vlbre9ef7a820b3f43c7bd3418bb62.uscom-central-1.c9dev1.oc9qadev.com");
JsonArray jArray = new JsonArray();
jArray.add(firstHost);
JsonObject jObj = new JsonObject();
jObj.add("virtual_hosts", jArray);
The first line converts your java string into a json string.
第一行将您的 java 字符串转换为 json 字符串。
In the next two steps, a json array is created and your string is added to it.
在接下来的两个步骤中,将创建一个 json 数组并将您的字符串添加到其中。
After that, a json object which is going to hold the array is created and the array is added with a key that makes the array accessible.
之后,将创建一个将保存数组的 json 对象,并为该数组添加一个使该数组可访问的键。
If you inspect the object, it looks exactly like you want to have it.
如果您检查对象,它看起来与您想要的完全一样。
There is no simple way in adding just a string to an JsonArray
if you want to use gson. If you need to add your string directly, you probably have to use another library.
JsonArray
如果您想使用gson ,则没有简单的方法可以将字符串添加到an。如果您需要直接添加字符串,则可能必须使用另一个库。
回答by Srikanth A
You can create a list of hosts and set the property in JSON.
您可以创建主机列表并在 JSON 中设置属性。
import org.json.simple.JSONObject;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<String> hosts = new ArrayList<String>();
hosts.add("vlbr-vlbre9ef7a820b3f43c7bd3418bb62.uscom-central-1.c9dev1.oc9qadev.com");
hosts.add("dummy.oc9qadev.com");
JSONObject jsonObj = new JSONObject();
jsonObj.put("virtual_hosts", hosts);
System.out.println("Final JSON String is--"+jsonObj.toString());
}
}
Output -
{ "virtual_hosts": ["vlbr-vlbre9ef7a820b3f43c7bd3418bb62.uscom-central-1.c9dev1.oc9qadev.com", "dummy.oc9qadev.com"] }
输出 -
{ "virtual_hosts": ["vlbr-vlbre9ef7a820b3f43c7bd3418bb62.uscom-central-1.c9dev1.oc9qadev.com", "dummy.oc9qadev.com"] }
回答by Ravi MCA
What you are trying to do is storing a JSONArray into a JSONObject. Because the key virtual_hosts
is going to contain a value as JSONArray.
您要做的是将 JSONArray 存储到 JSONObject 中。因为键virtual_hosts
将包含一个 JSONArray 形式的值。
Below code can help you.
下面的代码可以帮助你。
public static void main( String[] args ) {
JSONArray jsonArray = new JSONArray();
jsonArray.add( "vlbr-vlbre9ef7a820b3f43c7bd3418bb62.uscom-central-1.c9dev1.oc9qadev.com" );
JSONObject jsonObject = new JSONObject();
jsonObject.put( "virtual_hosts", jsonArray );
System.out.println( jsonObject );
}
Output:
输出:
{"virtual_hosts":["vlbr-vlbre9ef7a820b3f43c7bd3418bb62.uscom-central-1.c9dev1.oc9qadev.com"]}
Maven dependecny
Maven依赖
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>