1

i tried like that. but i did not get jsonArray and i do not know how can i do. I want to jsonArray as String. Can you help me?

public static func createInkitchenJson(additionId : Int64, orderId : Int64)
{
    let dicArray : NSMutableDictionary = NSMutableDictionary();
    let nsArray : NSMutableArray = NSMutableArray();

    for basket in BasketStatic.baskets
    {
        let dicObject : NSMutableDictionary = NSMutableDictionary();
        let model = basket.Product;
        dicObject.setValue(String(additionId), forKey: JsonHeader.ADDITION_ID);
        dicObject.setValue(String(orderId), forKey: JsonHeader.ORDER_ID);
        dicObject.setValue(BasketStatic.table?.getTableName(), forKey: JsonHeader.TABLE_NAME);
        dicObject.setValue(model.Id, forKey: JsonHeader.REQUEST_ID);
        dicObject.setValue(String(basket.RowCount), forKey: JsonHeader.ITEM_COUNT);
        dicObject.setValue(model.Name, forKey: JsonHeader.REQUEST_NAME);
        dicObject.setValue(model.getExtraToString(), forKey: JsonHeader.EXTRAS);
        dicObject.setValue(basket.getType(), forKey: JsonHeader.REQUEST_TYPE);
        dicObject.setValue(EMPTY_FIELD, forKey: JsonHeader.MENU_UNIQUE);
        dicObject.setValue(EMPTY_FIELD, forKey: JsonHeader.MENU_NAME);
        dicObject.setValue(EMPTY_FIELD, forKey: JsonHeader.MENU_ID);
        dicObject.setValue(String(basket.RowPrice), forKey: JsonHeader.PRICE);
        dicObject.setValue(WaiterModel.WAITER_ID, forKey: JsonHeader.CREATED_BY);
        nsArray.add(dicObject);
    }

    // here i want to convert NsMutableArray to jsonArray
    // jsonArray convert to string

} 
1

1 Answer 1

2

It's better to

let arr = [Item(/////),,,,,]
let data  = try? JSONEncoder().encode(arr) 
let str = String(data: data, encoding: .utf8)

struct Item:Decodable {

 // add properties 

}

OR

let data = try? JSONSerialization.data(withJSONObject: dic, options: [])

let str = String(data: data, encoding: .utf8)
Sign up to request clarification or add additional context in comments.

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.