2

I am new in unity and I need to download data from JSON. I successfully download JSON data but I am not able to parse the JSON. I have use Boomlagoon Asset to serialize my JSON. Here is my code.

    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"]);
        }
    }

and my JSON result is like follows,

{
    "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
        }
]
}

Now my question is that how can I get each values of id, workshop_name etc? Please help me to parse this JSON data. Thanks in advance.

Md. Sujan
  • 49
  • 1
  • 6
  • Paste your json [here](http://json2csharp.com/) to easily generate the proper classes. See the duplicate for how to do serialize/de-serialize json with that. – Programmer Aug 28 '17 at 10:07
  • This question was marked as an **exact** duplicate of an existing question. I would not say "EXACT" about this. @Programmer, also the answer is also different. Don't who Programmer is, but definitely he should not be so arbitrary. – David Aug 28 '17 at 10:13
  • @David It is a duplicate since it's asking about de-serializing json data but OP does not have to delete this as it will help people in the future. Please do not answer duplicate question. Flag them as duplicates instead. OP probably haven't heard about `JsonUtility`. The only time to recommend `JSONObject` is when the json starts with a numerical value. – Programmer Aug 28 '17 at 10:34

2 Answers2

2

Please refer to below code as reference:

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"

Here the JSON string (the_JSON_string) is as follows:

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

Note that according to our experience, SimpleJson works on both Android and iOS, and NewtonSoft does NOT work in some iOS devices. If your app is mobile app, you should not use NewtonJson.

David
  • 15,894
  • 22
  • 55
  • 66
-2
  • Add Newtonsoft.Json from nuGet package manager to your solution.
  • Add reference inside the unity file

  • in server side serialize your list like: JsonConvert.SerializeObject(your object);

  • in client side do like this : JSON.Parse(your result)