【发布时间】:2017-08-28 09:53:04
【问题描述】:
我是 Unity 新手,我需要从 JSON 下载数据。我成功下载了 JSON 数据,但无法解析 JSON。我已经使用 Boomlagoon Asset 来序列化我的 JSON。这是我的代码。
void Start() {
string url = "http://www.windmillinfotech.com/carlife/carlife_api/get_workshop";
WWWForm form = new WWWForm();
form.AddField("district", "Dhaka");
form.AddField("thana", "Mirpur");
form.AddField("service_type", "car");
WWW www = new WWW( url, form );
StartCoroutine (request(www));
}
IEnumerator request (WWW www) {
yield return www;
if(!string.IsNullOrEmpty(www.error)) {
print( "Error : " + www.error );
} else {
string serviceData = www.text;
JSONObject json = JSONObject.Parse(serviceData);
print ("\n\n\n\n"+json["workshop_dtls_result"]);
}
}
我的 JSON 结果如下,
{
"success": true,
"workshop_dtls_result": [
{
"id": "141",
"user_id": "",
"store_id": null,
"updated_by": null,
"workshop_name": "Okay Auto Engineering",
"workshop_email": "",
"workshop_address": "Section -10, Block - A, Plot - 9, Main Road, Mirpur, Dhaka-1216. Behind the graveyard, 01712978222",
"district": "Dhaka",
"thana": "Mirpur",
"post_code": "",
"contact_person": "Sabir Hossain",
"contact_number": "01712978222",
"alternative_number": "",
"service_type_car": "Car",
"service_type_bus": "",
"service_type_bike": "",
"workshop_photo_1": "",
"workshop_photo_2": "",
"workshop_photo_3": "",
"latitude": "",
"longitude": "",
"create_date": "2017-01-01",
"active_status": "Active",
"workshop_services": null,
"lubricants_available": "No",
"lubricant_products": null
},
{
"id": "142",
"user_id": "",
"store_id": null,
"updated_by": null,
"workshop_name": "Ali Automobile ",
"workshop_email": "",
"workshop_address": "Section -11, Block- D, Avenue-1 Plot-14, Mob: 01925920115",
"district": "Dhaka",
"thana": "Mirpur",
"post_code": "",
"contact_person": "Mohammad Ali",
"contact_number": "01925920115",
"alternative_number": "",
"service_type_car": "Car",
"service_type_bus": "",
"service_type_bike": "",
"workshop_photo_1": "",
"workshop_photo_2": "",
"workshop_photo_3": "",
"latitude": "",
"longitude": "",
"create_date": "2017-01-01",
"active_status": "Active",
"workshop_services": null,
"lubricants_available": "No",
"lubricant_products": null
}
]
}
现在我的问题是如何获取 id、workshop_name 等的每个值?请帮我解析这个 JSON 数据。 提前致谢。
【问题讨论】:
-
粘贴您的 json here 以轻松生成正确的类。请参阅副本以了解如何使用它对 json 进行序列化/反序列化。
-
此问题被标记为与现有问题的完全重复。我不会对此说“完全”。 @Programmer,答案也不同。别说程序员是谁,但绝对不能这么武断。
-
@David 这是重复的,因为它询问反序列化 json 数据,但 OP 不必删除它,因为它将在未来帮助人们。请不要回答重复的问题。而是将它们标记为重复项。 OP 可能还没有听说过
JsonUtility。唯一推荐JSONObject的时候是json以数值开头的时候。