简体   繁体   中英

How do i parse this JSON object in Swift using SwiftyJSON?

I have created a JSON array from PHP and MySQL. I have used Alamofire to get the response from the server, however, i am stuck on how to use SwiftyJSON to parse the data, so that i can then put it into a custom object array then load it into a tableView.

My JSON object looks like this:

     {
    person =     {
        "average_price" = "7430.83";
        distance = "3012.984699624479";
        "person_id" = 9;
        "person_name" = Rerobepicator;
    };
}
{
    person =     {
        "average_price" = "2281.35";
        distance = "3780.0428863723305";
        "person_id" = 7;
        "person_name" = Vartanentor;
    };
}
{
    person =     {
        "average_price" = "20.00";
        distance = "3923.2963365576284";
        "person_id" = 16;
        "person_name" = bangbang;
    };
}
{
    person =     {
        "average_price" = "7430.83";
        distance = "3953.934665981184";
        "person_id" = 9;
        "person_name" = Rerobepicator;
    };
}
{
    person =     {
        "average_price" = "747.86";
        distance = "4040.1432041597536";
        "person_id" = 8;
        "person_name" = Happickamex;
    };
}
{
    person =     {
        "average_price" = "4967.73";
        distance = "4288.827177318351";
        "person_id" = 3;
        "person_name" = Klibanollover;
    };
}
{
    person =     {
        "average_price" = "2281.35";
        distance = "4393.581790994688";
        "person_id" = 7;
        "person_name" = Vartanentor;
    };
}
{
    person =     {
        "average_price" = "20.00";
        distance = "4792.883669320238";
        "person_id" = 16;
        "person_name" = bangbang;
    };
}
{
    person =     {
        "average_price" = "9857.78";
        distance = "6374.9873653967";
        "person_id" = 4;
        "person_name" = Admunover;
    };
}
{
    person =     {
        "average_price" = "747.86";
        distance = "6822.324630020093";
        "person_id" = 8;
        "person_name" = Happickamex;
    };
}
{
    person =     {
        "average_price" = "9494.88";
        distance = "7177.263972744869";
        "person_id" = 10;
        "person_name" = Thrukilanicator;
    };
}
{
    person =     {
        "average_price" = "8570.38";
        distance = "8208.371313460493";
        "person_id" = 5;
        "person_name" = Undudplazz;
    };
}

The reason why it looks like the above is because i have done this to split up each JSON object:

            if let personobject = JSON as? Array<AnyObject> {

                for person in personobject {
                    println(person)
                }
            }

    }

I now need to load up each of these into my custom object which looks like this:

class shop {

var shop_name : String?
var shop_type : String?
var averageprice : Float?

init() {

}

}

Then i will load this into my array of Person objects and load it into my tableView with a custom cell object.

Thank you :)

Your first issue is that your JSON is not valid at all, so you probably going to have some trouble to parse it with SwiftyJSON,

You can validate your JSON with this link : jsonformatter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM