xcode解析json表视图

时间:2016-08-23 13:10:31

标签: json xcode tableview

您好我有一个应用程序,其中有一个UITableView向我展示了json的结果。我需要一个帮助来显示json对象的单元格。这是json的结果:

{
followers =     (
            {
        "_id" = 
        timestamp = 
        user =             {
            "__v" = 0;
            "_id" = 
            birthdate = 
            coverImageURL = "<null>";
            "f_enabled" = 1;
            first = 
            followStatus = 0;
            followers = 0;
            following = 1;
            id = 
            idDatabase = 8;
            lang = it;
            last = 
            profileImageURL = 
            second = "<null>";
            username = 
            views = 10;
        };
    },
            {
        "_id" = 
        timestamp = 
        user =             {
            "__v" = 0;
            "_id" = 
            birthdate = "<null>";
            coverImageURL = "<null>";
            "f_enabled" = 1;
            first = "<null>";
            followStatus = 0;
            followers = 0;
            following = 2;
            id = 
            idDatabase = 
            lang = "it-IT";
            last = "<null>";
            profileImageURL = "<null>";
            second = "<null>";
            username = 
            views = 0;
        };
    },
            {
        "_id" = 
        timestamp = 
        user =             {
            "__v" = 0;
            "_id" = 
            birthdate = 
            coverImageURL = 
            "f_enabled" = 1;
            first = Mark;
            followStatus = 0;
            followers = 0;
            following = 8;
            id = 
            idDatabase = 72;
            lang = it;
            last = Fat;
            profileImageURL = 
            second = "<null>";
            thumbImageURL = 
            username = 
            views = 10;
        };
    },
            {
        "_id" = 
        timestamp = 
        user =             {
            "__v" = 0;
            "_id" = 
            birthdate = "<null>";
            coverImageURL = 
            "f_enabled" = 1;
            first = "<null>";
            followStatus = 0;
            followers = 0;
            following = 6;
            id = 
            idDatabase =
            lang = it;
            last = "<null>";
            profileImageURL = 
            second = "<null>";
            thumbImageURL = 
            username = 
            views = 0;
        };
    },
            {
        "_id" = 
        timestamp = 
        user =             {
            "__v" = 0;
            "_id" = 
            birthdate = 
            coverImageURL = 
            "f_enabled" = 1;
            first = 
            followStatus = 0;
            followers = 0;
            following = 6;
            id = 
            idDatabase = 
            lang = it;
            last =
            profileImageURL = 
            second = "<null>";
            thumbImageURL = 
            username = 
            views = 0;
        };
    },
            {
        "_id" = 
        timestamp = 
        user =             {
            "__v" = 0;
            "_id" = 
            birthdate = 
            coverImageURL = 
            "f_enabled" = 1;
            first = 
            followStatus = 0;
            followers = 4;
            following = 11;
            id = 
            idDatabase = 29;
            lang = "it-IT";
            last = 
            profileImageURL = 
            second = "<null>";
            username = 
            views = 44;
        };
    }
);

}

这是我用来解析json的函数:

func loadData()
{
    let url = NSURL(string: "" )
    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("Bearer \(AppPreferences.getAuthToken())", forHTTPHeaderField: "Authorization")

    let dm = DataManager();
    dm.initTotalStringReq();

    dm.addParamStringReq("lang", value: "it-IT",comma: true)
    dm.addParamStringReq("id", value:"",comma: false)

    dm.finishTotalStringReq()
    print(dm)

    request.HTTPBody = dm.getTotalStringReq().dataUsingEncoding(NSUTF8StringEncoding);

    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request) { data, response, error in
        if let response = response, data = data {
            print(response)
            let dataString = "[" + String(data: data, encoding: NSUTF8StringEncoding)! + "]";
            let nsdata:NSData = dataString.dataUsingEncoding(NSUTF8StringEncoding)!
            self.item = DataManager.parseJSON(nsdata);
            print(self.item);

            dispatch_async(dispatch_get_main_queue(), {
                self.tableView.reloadData()
            })
        } else {
            print(error)
        }
    }

    task.resume()

}
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {



    if ((self.item) != nil)
    {

        return (self.item[0]["result"]!!["followers"] as? NSArray)!.count;
    }
    else
    {
        return 0;
    }

   // return item.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("followerCell", forIndexPath: indexPath) as? FollowerTableViewCell



    let row = indexPath.row;


    cell!.nameFollower.text = (self.item[0]["result"]!!["followers"] as! NSArray)[row]["user"] as? String;
    print("2")

    //UtilityFunc.loadImages((cell!.imgProfile)!, url: ((self.dataLikes[0]["result"]!!["likes"] as! NSArray)[row]["profileImageURL"] as? String)!);


    return cell!
}

我需要在&#34; user&#34;中显示对象。 NSDictionary

1 个答案:

答案 0 :(得分:0)

试试这个

    let task = session.dataTaskWithRequest(request) { data, response, error in
            if let response = response, data = data {
                print(response)
                let dataString = "[" + String(data: data, encoding: NSUTF8StringEncoding)! + "]";
                let nsdata:NSData = dataString.dataUsingEncoding(NSUTF8StringEncoding)!
if let arrTemp = DataManager.parseJSON(nsdata).valueforKey("followers") as? NSArray{
                self.item = arrTemp;
                print(self.item);

                dispatch_async(dispatch_get_main_queue(), {
                    self.tableView.reloadData()
                })
}
            } else {
                print(error)
            }
        }

在Cell for Row中

if let strBithdate = self.item.objectAtIndex[indexPathRow].valueForKey("birthdate") as? string
{
lblbirthdate = strBithdate
}