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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With