所以当我尝试
时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"}]}"
我收到错误,因为它包含括号,我如何将其设置为字符串? 谢谢你的帮助。
答案 0 :(得分:10)
括号不是问题 - 引号是。例如,这很好:
String braces = "{}";
引号("
)用于终止字符串,因此您需要使用\
转义它:
String sentence = "My son said, \"Hello, world!\" and then ran away.";
创建一个字符串:
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 = '\'';
我鼓励你不要在你的代码中直接存储大量的文本 - 如果你把它存储为在执行时加载数据的文本文件,它通常更具可读性。
答案 1 :(得分:0)
让我举个例子:
String str = "Hello \"joe\"!";
答案 2 :(得分:0)
这是一个json对象,更好地格式化它。为什么不使用某些JSON构建器从Object创建String。并将此对象描述为您在此处看到的内容。
杰克逊是一个很好的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;
}
-
"{"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"}]
}"