0

Can Anyone Help me with this

my data after parsing a JSON URL is

{
    AREA =     (
                {
            "area_name" = "Bhaktamadhu Nagar";
            "city_id" = 4;
            id = 31;
            price = "100.00";
        },
                {
            "area_name" = "Gandamunda";
            "city_id" = 4;
            id = 32;
            price = "100.00";
        }
);
}

and there is a lot more.

I want to fetch only area_name and price values in an array

my code is something like that

do { 
let parsedData = try JSONSerialization.jsonObject(with: data!, options: .mutableLeaves) as! NSDictionary
print(parsedData)}

I am getting my Upper format in the parsedData

What is the exact code for getting my area_name and price which should store in two separate arrays as aname[] and price[]

Please don't mark it as a duplicate already searched a lot before posting this.

2 Answers 2

1

Your JSON data is converted into [String: AnyObject].

AREA data is [[String: AnyObject]] so create a [String: AnyObject] array. and getting a one by one value from array.

How to fetch JSON data from a url using URLSession?

try this code. it's helpfull

URLSession.shared.dataTask(with: url) { (data, response, error) in

        if let jsonData = data {
            do {
                let parsedData = try JSONSerialization.jsonObject(with: jsonData, options: .mutableLeaves) as! [String: AnyObject]
                if let area = parsedData["AREA"] as? [[String: AnyObject]] {
                    for a in area {
                        areaNameArr.append(a["area_name"])
                        priceArr.append(a["price"])
                        print(a)

                    }
                }
            }
            catch let error {
                debugPrint(error)
            }
        }
        else {
            debugPrint(error as Any)
        }

    }.resume()
Sign up to request clarification or add additional context in comments.

5 Comments

I am getting this error now: --- Editor placeholder in the source file in both the print statement.
Is data printing or not in debug console?
No, while writing the code only this error is there
How Can I @AshvinGudaliya
Now I have found the values in my console but not in array please help me?
-1

Use the SwiftyJSON Lib.

It’s easy and fast.

I am using it and it’s very helpful in this way:

let session = URLSession(configuration: URLSessionConfiguration.ephemeral)

    self.Task = session.dataTask(with: RequestLink as URLRequest , completionHandler: { (data,response,error) in
        if error != nil {
            print(error as Any)
        }

        let ReadJson4Rest = JSON(data: data!)
        if let Rest_Details = ReadJson4Rest["Result"].array{
            for Details in Rest_Details {

                let Comment  =  Details.dictionaryValue["Comment"]!
                let UserName =  Details.dictionaryValue["User_ID"]!

                if Comment != nil {

                    let FirstChar = UserName.description.characters.first
                    self.GetUserImage(UserName: UserName.string! ,AlphabetCat: (FirstChar?.description)!)
                    DispatchQueue.main.async {
                        self.CommentValue.append(Comment.string!)
                        self.UserNames.append(UserName.string!)
                        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Load"), object: nil)
                    }
                }
            }
        }
    })
    self.Task?.resume() 
}

1 Comment

Thanks for this I will do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.