在Android中将JSON响应转换为Array格式



我正在从服务器获得JSONArray格式的响应。我无法检索数组的内容,我的JSONArray也没有方括号。

我在服务器端中以json_encode($array)的形式传递php中的响应

response {
community = “worker”
communitystr = "<null>";
workspace = abs;
email = "<null>";
admin = false;
persona = "<null>";
userinfo =  {
info =   {
contact1 =    {
firstname = “jon”;
lastname  = “Doe”
phone = “9885678905”;
objectname = contact;
id = 9;
};
event1 =      {
eventname = “party”;
description = "";
order = 6;
id = 4;
objectname = events;
};
files =       {
filename = “sample”;
description = "";
order = 11;
id = 11;
objectname = files;
};
};
};
};

我检查了许多链接,所有链接都使用了JSONObject()。但同样的做法对我不起作用。

如何获取此JSON响应中的每个值?

您必须使用:而不是=
,而不是;

注意以下格式:

{
"Herausgeber": "Xema",
"Nummer": "1234-5678-9012-3456",
"Deckung": 2e+6,
"Waehrung": "EURO",
"Inhaber": 
{
"Name": "Mustermann",
"Vorname": "Max",
"maennlich": true,
"Hobbys": [ "Reiten", "Golfen", "Lesen" ],
"Alter": 42,
"Kinder": [],
"Partner": null
}
}

您的代码似乎更像JavaScript对象:-)

您的响应不是有效的JSON对象。

您可以通过一些在线工具验证JSON,例如http://jsonlint.com/

完整的规范可以在RFC 7159中找到https://www.rfc-editor.org/rfc/rfc7159.

基本上,您应该了解如何以正确的方式将值编码为JSON格式。为此,您可以使用JSON_encode()将PHP数组引用为JSON数组;

最新更新