简体   繁体   中英

Invalid character constant JSON

public class Test {

    public static void main(String[] args) throws ClientProtocolException, IOException {
    HttpClient client = new DefaultHttpClient();

    JSONObject json = new JSONObject();
    json.put('set_boot' : True);
    json.put('synchronous': True),
    json.put('filters',filterList);

    List filterList = new ArrayList();
    filterList.put(6005076802818994A0000000000009DD);

    json.put('volumes':volumeList);

    List volumeList = new ArrayList();
    volumeList.add('*all');

    json.add( 'allow_unsupported': True);
    StringEntity se = new StringEntity( json.toString());
    System.out.println(se);
   }
}

I am trying to add values to convert into a JSON query like:

{
    'set_boot': True,
    'synchronous': True,
    'filters': {
        'host_refs': [u '6005076802818994A0000000000009DD']
    },
    'volumes': ['*all'],
    'allow_unsupported': True
}

But Eclipse is giving me error Invalid Character constant on line

json.put('set_boot' : True);

I have tried writing a few other words also like

json.put('set' : True);

But it still gives me the same error.

If this is meant to be Java, you want:

json.put("set_boot", true);
json.put("synchronous", true);

(There are similar problems later on.)

Note:

  • String literals are in double-quotes, not single-quotes in Java
  • You're calling a method with two arguments - use a comma to separate the arguments
  • The boolean true value in Java is true , not True . You could use "True" to set a string value if you want
  • You're trying to use filterList before it's declared
  • You're trying to call put on a List , when that method doesn't exist... you meant add
  • Your literal of 6005076802818994A0000000000009DD is invalid. Did you mean to use a string literal?

These are all just matters of the Java language, and have nothing to do with JSON in themselves.

Besides your snytax errors in Java, your JSON example is invalid syntax as well. Check it using jsonlint or other services. It should look like the following:

{
    "set_boot": true,
    "synchronous": true,
    "filters": {
        "host_refs": [
            "6005076802818994A0000000000009DD"
        ]
    },
     "volumes": [
        "*all"
    ],
    "allow_unsupported": true
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM