如何制作包含括号的字符串?-JAVA

如何制作包含括号的字符串?-JAVA,java,string,Java,String,所以当我尝试 String string1 = "{"needs_reward":false,"has_voted":false,"sites":[{"id":3922,"name":"RuneTopList","has_voted":false,"vote_url":"http://api.runetoplist.com/vote/out?siteid=3922"},{"id":4613,"name":"RuneLocus","has_voted":false,"vote_url":"http

所以当我尝试

String string1 = "{"needs_reward":false,"has_voted":false,"sites":[{"id":3922,"name":"RuneTopList","has_voted":false,"vote_url":"http://api.runetoplist.com/vote/out?siteid=3922"},{"id":4613,"name":"RuneLocus","has_voted":false,"vote_url":"http://api.runetoplist.com/vote/out?siteid=4613"},{"id":4339,"name":"UltimatePrivateServers","has_voted":true,"vote_url":"http://api.runetoplist.com/vote/out?siteid=4339"},{"id":4340,"name":"TopG","has_voted":true,"vote_url":"http://api.runetoplist.com/vote/out?siteid=4340"},{"id":4341,"name":"MMORPGToplist","has_voted":true,"vote_url":"http://api.runetoplist.com/vote/out?siteid=4341"},{"id":4622,"name":"Rune-Server","has_voted":true,"vote_url":"http://api.runetoplist.com/vote/out?siteid=4622"},{"id":4623,"name":"GTop100","has_voted":true,"vote_url":"http://api.runetoplist.com/vote/out?siteid=4623"},{"id":4828,"name":"GamingTopList","has_voted":true,"vote_url":"http://api.runetoplist.com/vote/out?siteid=4828"},{"id":4829,"name":"RSPS-List","has_voted":true,"vote_url":"http://api.runetoplist.com/vote/out?siteid=4829"},{"id":4861,"name":"Top100Arena","has_voted":true,"vote_url":"http://api.runetoplist.com/vote/out?siteid=4861"}]}"
我得到错误是因为它包含方括号,我如何将其设置为字符串?
谢谢你的帮助。

括号没有问题-引号是。例如,这很好:

String braces = "{}";
引号用于终止字符串,因此需要使用\:

这将创建一个字符串:

My son said, "Hello, world!" and then ran away.
如果文本中需要\,也需要转义,但不需要正斜杠:

String slashes = "Backslash: \\ and forward slash: /";
您不需要在字符串中转义单引号,但需要在字符文字中转义它们,否则“将是文字的结尾:

String text = "I don't need a backslash";
char c = '\'';

无论如何,我建议您不要在代码中直接存储大量文本——如果您将其存储为文本文件,并在执行时从中加载数据,通常可读性会更高。

括号没有问题-引号是。例如,这很好:

String braces = "{}";
引号用于终止字符串,因此需要使用\:

这将创建一个字符串:

My son said, "Hello, world!" and then ran away.
如果文本中需要\,也需要转义,但不需要正斜杠:

String slashes = "Backslash: \\ and forward slash: /";
您不需要在字符串中转义单引号,但需要在字符文字中转义它们,否则“将是文字的结尾:

String text = "I don't need a backslash";
char c = '\'';

不过,我建议您不要在代码中直接存储大量文本——如果您将其存储为文本文件,并在执行时从中加载数据,通常可读性会更高。

让我给您举个例子:

String str = "Hello \"joe\"!";

让我举个例子:

String str = "Hello \"joe\"!";

这是一个json对象,格式更好。为什么不使用一些JSON构建器从对象创建字符串呢。让这个物体描绘你在这里看到的东西

Jackson是一个很好的api

class Reward {
    boolean needs_reward = false;
    boolean has_voted = false;
    List<Sites> sites = new ArrayList();
}

class Sites {
   int id;
   String name;
   boolean has_voted;
   String vote_url;
}

这是一个json对象,格式更好。为什么不使用一些JSON构建器从对象创建字符串呢。让这个物体描绘你在这里看到的东西

Jackson是一个很好的api

class Reward {
    boolean needs_reward = false;
    boolean has_voted = false;
    List<Sites> sites = new ArrayList();
}

class Sites {
   int id;
   String name;
   boolean has_voted;
   String vote_url;
}

你的问题是报价。将每个引号更改为\除了第一个和最后一个引号外,将所有其他引号转义为\可能会有所帮助。您的问题在于引号。将每个引号改为\除了第一个和最后一个引号,将所有其他引号转义为\可能会有所帮助。我是你的超级粉丝!:我是你的超级粉丝