0
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);
   }
}

我正在尝试添加值以转换为 JSON 查询,例如:

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

但是 Eclipse 在线给我错误 Invalid Character constant

json.put('set_boot' : True);

我试过写一些其他的词也喜欢

json.put('set' : True);

但它仍然给我同样的错误。

4

2 回答 2

3

如果这是 Java,您需要:

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

(后面有类似的问题。)

笔记:

  • 字符串文字在双引号中,而不是 Java 中的单引号
  • 您正在调用带有两个参数的方法 - 使用逗号分隔参数
  • Java 中的布尔真值是true,不是True。如果需要,您可以使用"True"设置字符串
  • 您正在尝试filterList在声明之前使用
  • 当该方法不存在时,您正在尝试调用puta ......您的意思是Listadd
  • 您的文字 of6005076802818994A0000000000009DD无效。您的意思是使用字符串文字吗?

这些都只是 Java 语言的事情,与 JSON 本身无关。

于 2013-10-29T07:28:11.677 回答
1

除了 Java 中的语法错误之外,您的 JSON 示例也是无效语法。使用jsonlint或其他服务检查它。它应该如下所示:

{
    "set_boot": true,
    "synchronous": true,
    "filters": {
        "host_refs": [
            "6005076802818994A0000000000009DD"
        ]
    },
     "volumes": [
        "*all"
    ],
    "allow_unsupported": true
}
于 2013-10-29T07:32:54.953 回答