我希望获得" name"的价值来自以下机构:
$collection->find(array('age_premium.age'=>'65'));
我尝试过以下代码:
[
{
"name": "ENTITY_CHANNELS",
"description": "The list of channels use to deliver through.",
"metadata": "{}"
},
{
"name": "INTAKE_SFTP",
"description": null,
"metadata": "{\n \"fields\":[\n {\n
\"field_name\":\"user_uat\",\n \"field_type\":\"string\",\n
\"field_required\":true, \n\t \"field_description\": \"UAT User name\"\n
},\n {\n \"field_name\":\"user_prod\",\n
\"field_type\":\"string\",\n \"field_required\":false, \n\t
\"field_description\": \"Production User name\"\n },\n {\n
\"field_name\":\"folder\",\n \"field_type\":\"string\",\n
\"field_required\":true, \n\t \"field_description\": \"Folder\"\n },\n
{\n \"field_name\":\"address_UAT\",\n \"field_type\":\"string\",\n
\"field_required\":true, \n\t \"field_description\": \"UAT address\"\n
},\n {\n \"field_name\":\"address_PROD\",\n
\"field_type\":\"string\",\n \"field_required\":false, \n\t
\"field_description\": \"Production address\"\n },\n {\n
\"field_name\":\"port\",\n \"field_type\":\"integer\",\n
\"field_required\":true, \n\t \"field_description\": \"Port\"\n },\n
{\n \"field_name\":\"protocol_version\",\n
\"field_type\":\"version\",\n \"field_required\":false, \n\t
\"field_description\": \"Protocol version\"\n }\n ]\n}"
}
]
但我一直在收到错误:JSONObject文本必须以' {'错误。我想提取" name"的价值。有人能帮助我吗?
答案 0 :(得分:0)
谢谢大家的意见。我意识到我的错误是什么。我用过:
String settingsTypeData = response.body().toString();
以下代码将解决我的问题:
String settingsTypeData = response.body().string();
JSONArray settingsTypeArray = new JSONArray(settingsTypeData);
String firstSettingType = settingsTypeArray.getJSONObject(0).getString("name");
Assert.assertEquals(firstSettingType, "ENTITY_CHANNELS");
String secondSettingType = settingsTypeArray.getJSONObject(1).getString("name");
Assert.assertEquals(secondSettingType, "INTAKE_SFTP");
答案 1 :(得分:-1)
使用JSONArray代替JSONObject, 因为上面的整个json是JSONArray。 然后迭代此JSONArray并从每个JSONObject
获取名称 String settingsTypeData = response.body().toString();
try {
JSONArray settingsTypeArray = new JSONArray(settingsTypeData);
for(int i=0;i<settingsTypeArray.length();i++){
String name = settingsTypeArray.getJSONObject(i).getString("name");
Log.d(TAG, "onActivityResult: name = "+name);
}
} catch (JSONException jex){
jex.printStackTrace();
}