如何在统一C#中解析JSON中的数据?

时间:2017-08-28 09:53:04

标签: c# json unity3d

我是团结的新手,我需要从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数据。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

请参考以下代码作为参考:

using SimpleJSON;
var N = JSON.Parse(the_JSON_string);       
var versionString = N["version"].Value;        // versionString will be a string containing "1.0"
var versionNumber = N["version"].AsFloat;      // versionNumber will be a 
float containing 1.0
var name = N["data"]["sampleArray"][2]["name"];// name will be a string containing "sub object"

这里的JSON字符串(the_JSON_string)如下:

    {
    "version": "1.0",
    "data": {
        "sampleArray": [
            "string value",
            5,
            {
                "name": "sub object"
            }
        ]
    }
}

请注意,根据我们的经验,SimpleJson适用于Android和iOS,而NewtonSoft不适用于某些iOS设备。如果您的应用是移动应用,则不应使用NewtonJson。

答案 1 :(得分:-2)

  • 将nuGet包管理器中的 Newtonsoft.Json 添加到您的解决方案中。
  • 在统一文件中添加引用
    服务器端的
  • 序列化您的列表,如: JsonConvert.SerializeObject(您的对象);

  • 客户端的
  • 是这样的: JSON.Parse(您的结果)