How to correctly send array with objects in JSON Swift iOS

Viewed 815

Hello I am beginner and trying to understand how to send an array with objects. Does the server understand what an array is like Int, Strings or Booleans? Do you have to send the array in a string for JSON? What I do not understand?

var productsResult = ""
let encoder = JSONEncoder()
let productObject = ProductUsed(name: "Custom name", reason: "This Reason", apply_way: "Intravenous infusion", dosing: "2x", date_start: "22-02-1999", date_end: "22-03-2003")
        
        do {
            let result = try encoder.encode([productObject])
            if let resultString = String(data: result, encoding: .utf8) {
                print(resultString)
                productsResult = resultString
            }
        } catch {
            print(error)
        }
        
json["products_used"] = productsResult

And I sent to server with parameters like this:

parameters:  ["pregnancy_week": 0, "body_height": 198, "initials": "John Appleseed", "heavy_effect": false, "sex": "Male", "pregnancy": false, "month_of_birth": 3, "reaction": "No option checked", "additional_info": "Eeee", "products_used": "[{\"date_end\":\"22-03-2003\",\"dosing\":\"2x\",\"date_start\":\"22-02-1999\",\"apply_way\":\"Intravenous infusion\",\"name\":\"Custom name\",\"reason\":\"This Reason\"}]", "description": "Eeee", "result": "Recovery without lasting consequences", "year_of_birth": 1983, "day_of_birth": 11, "issue_date": "15-11-2020", "body_weight": 78]

but printed "resultString" in log and looks good...

[{"date_end":"22-03-2003","dosing":"2x","date_start":"22-02-1999","apply_way":"Intravenous infusion","name":"Custom name","reason":"This Reason"}]

What's wrong in my code and why I have " \ " between words in "products_used" key?

1 Answers

JSON, unlike XML, does not specify the structure and type explicitly. This means that the server must know what JSON data to expect.

In JSON there are a few value types (https://www.w3schools.com/js/js_json_syntax.asp):

  • a string
  • a number
  • an array
  • a boolean
  • null
  • a JSON object (a dictionary with tags like { "first" : "John", "last" : "Doe" }). This allows nesting.

A JSON object is a set of tag-value pairs. The tag and value are separated by : and pairs are separated by ,.

An array is a list of JSON values. So for example [ "hello", world" ] is a JSON array with 2 strings and [ 12, 54 ] is a JSON array with two numbers.

Your parameter list ["pregnancy_week": 0, "body_height": 198, ... is not an array but a dictionary instead. A Swift dictionary is translated to an JSON object, not a JSON array.

The \ you see printed acts as escape character. This escape character is used to allow you to have a " inside the string.

That's just a few things that I hope will help understand things a bit better. Your questions are pretty basic, which is fine and it's great you want to understand things. But instead of us explaining everything here, I think it would be best if you'd read about the structure of JSON and how JSON works in Swift on your own.

Related