3

I want do this JSON in Swift. But i cant do this....

{
    "room": "Platinum",
    "products": [{
        "name": "Agua",
         "quantity": 2
},
{   
    "name":"Cafe",
    "quantity": 4
}],
    "observation": "",
    "date": "2016-08-15 12:00:00"
}

My swift code returns this:

{
    date = "2016-08-25 18:16:28 +0000";
    observation = "";
    products =     (
            {
                name = cafe;
                quantity = 1;
            }
    );
    room = Platinium;

This is my code:

let para:NSMutableDictionary = NSMutableDictionary()
let prod: NSMutableDictionary = NSMutableDictionary()

para.setValue(String(receivedString), forKey: "room")
para.setValue(observationString, forKey: "observation")
para.setValue(stringDate, forKey: "date")

for product in products{
    prod.setValue(product.name, forKey: "name")
    prod.setValue(product.quantity, forKey: "quantity")
    para.setObject([prod], forKey: "products")
}

This is my input:

{
    name = coffe;
    quantity = 2;
}
{
    name = cappuccino;
    quantity = 1;
}

This is output

{
    date = "2016-08-25 18:52:30 +0000";
    observation = "";
    products =     (
            {
                name = cappuccino;
                quantity = 1;
            }
    );
    room = Platinium;
}

I created a request to send the two products, but the code prints only the last product.

2
  • Little confusing, what is your input (data you work with) a what is the output you mention? Consider to mark input/output staff in your question, please Commented Aug 25, 2016 at 18:51
  • You're overwriting the keys in the same dictionary - prod. Commented Aug 25, 2016 at 18:52

2 Answers 2

10

Create a new prodArray array which holds all the prod dictionary(name and quantity.) Set this prodArray for para array corresponding to products key.

Issue in your code:- In your forin loop, you are over-riding the value corresponding to "products" key.

let para:NSMutableDictionary = NSMutableDictionary()
let prodArray:NSMutableArray = NSMutableArray()

para.setValue(String(receivedString), forKey: "room")
para.setValue(observationString, forKey: "observation")
para.setValue(stringDate, forKey: "date")

for product in products
{
    let prod: NSMutableDictionary = NSMutableDictionary()
    prod.setValue(product.name, forKey: "name")
    prod.setValue(product.quantity, forKey: "quantity")
    prodArray.addObject(prod)
}

para.setObject(prodArray, forKey: "products")
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I am using your code. However; when I send in the server all the prod values are showng as single entities, not group like we are assigning.
All the values are assigned to a dictionary. This dictionary is holding an array of the dictionaries. Is it clear now?
Is there a way I can store multiple key in one dictionary ? Like for ex: have { name: 'some name' , quanity: '2'} , { ...
Thanks, I was stuck in similar type of problem. This works like charm
3

You need to move let prod: NSMutableDictionary = NSMutableDictionary() inside the for loop and move para.setObject([prod], forKey: "products") after the for loop, but you also need to change so you create and add each prod to an array in the loop and then add that array to para after the loop.

Your current code keeps replacing things rather than adding so at the end you only have the last one.

Comments

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.