我有一个像 string[] Sentences 这样的字符串数组,它由每个索引中的一个句子组成,例如 This is the first message
in sentences[0]
和 这是
,依此类推。我将信息发送到服务器进行情感分析的java代码如下:sentences[1]
中的第二条消息
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(
"[ " +
"\"Can't wait for the movie\"," +
"\"Countdown! Only ten days remaining!\" " +
"]");
out.flush();
out.close();
如何用长度为 n 的字符串数组替换代码中上面的文本?
最佳答案
使用Gson库,它将Java对象转换为Json字符串
String[] sentences=new String[3];
sentences[0]="Hi";
sentences[1]="Hello";
sentences[2]="How r u?";
Gson gson=new GsonBuilder().create();
String jsonArray=gson.toJson(sentences);
//["Hi","Hello","How r u?"]
out.write(jsonArray);
out.flush();
out.close();
关于java - 如何将 String 数组转换为 Json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35250698/