So when i try
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"}]}"
I get errors because it contains brackets, how would i set this as a string? Thanks for the help.
The brackets aren't problem - the quotes are. For example, this is fine:
String braces = "{}";
The quote ( "
) is used to terminate a string, so you need to escape it with a \\
:
String sentence = "My son said, \"Hello, world!\" and then ran away.";
That creates a string of:
My son said, "Hello, world!" and then ran away.
If you need a \\
in your text, you need to escape that, too - but not forward slashes:
String slashes = "Backslash: \\ and forward slash: /";
You don't need to escape single quotes within a string, but you do need to escape them within a character literal (as '
would otherwise be the end of the literal):
String text = "I don't need a backslash";
char c = '\'';
I would encourage you not to store a lot of text directly in your code anyway though - it's generally more readable if you store it as text files which you load the data from at execution time.
让我给你举个例子:
String str = "Hello \"joe\"!";
This is a json object, formatting it better. Why don't you use some JSON builders to create String from Object. And have this object depicting what you see here.
Jackson is a good 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"}]
}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.