简体   繁体   中英

how to write correct JSON string file in swift

i have created a JSON string file which contains

{
[{"teamName":"Arsenal",
 "image":"Arsenal",
 "nextMatch":"in 2 days",
 "matches":[{"oppositeTeam":"teamName",
            "matchTimings":"121212",
            "matchId":"ID 213432"},
            {"oppositeTeam":"teamName",
            "matchTimings":"121212",
            "matchId":"ID 213432"}],

 "fixtures": {"oppositeTeam":"teamName",
 "oppositeTeamScore":"7",
 "homeTeamScore":"4",
 "homeTeamCards":"True",
 "oppositeTeamCards":"false",
 "fixtureId":"ID 213432”}
 }},
 {"teamName":"Chelsea",
 "image":"Chelsea",
 "nextMatch":"in 2 days",
 "matches":{"oppositeTeam":"teamName",
            "matchTimings":"121212",
            "matchId":"ID 213432"},

 "fixtures": {"oppositeTeam":"teamName",
 "oppositeTeamScore":"7",
 "homeTeamScore":"4",
 "homeTeamCards":"True",
 "oppositeTeamCards":"false",
 "fixtureId":"ID 213432”}
 }},{
 "teamName":"India",
 "image":"India",
 "nextMatch":"in 2 days",
 }
 ] }

but when i checked this JSON on online Json reader it shows many errors and i am new to json parsing and dont know how to correct json

Correct JSON syntax:

{
   "teams":[
      {
         "teamName":"Arsenal",
         "image":"Arsenal",
         "nextMatch":"in 2 days",
         "matches":[
            {
               "oppositeTeam":"teamName",
               "matchTimings":"121212",
               "matchId":"ID 213432"
            },
            {
               "oppositeTeam":"teamName",
               "matchTimings":"121212",
               "matchId":"ID 213432"
            }
         ],
         "fixtures":{
            "oppositeTeam":"teamName",
            "oppositeTeamScore":"7",
            "homeTeamScore":"4",
            "homeTeamCards":"True",
            "oppositeTeamCards":"false",
            "fixtureId":"ID 213432"
         }
      },
      {
         "teamName":"Chelsea",
         "image":"Chelsea",
         "nextMatch":"in 2 days",
         "matches":{
            "oppositeTeam":"teamName",
            "matchTimings":"121212",
            "matchId":"ID 213432"
         },
         "fixtures":{
            "oppositeTeam":"teamName",
            "oppositeTeamScore":"7",
            "homeTeamScore":"4",
            "homeTeamCards":"True",
            "oppositeTeamCards":"false",
            "fixtureId":"ID 213432"
         }
      },
      {
         "teamName":"India",
         "image":"India",
         "nextMatch":"in 2 days"
      }
   ]
}

Your errors were not having a key for the overall array, using typographer quotes on the closing of the matchIds and extra closing braces.

I want to learn so that i can do it by myself in future

Well, to write a valid JSON 100% without any big problems, i would suggest Codable ,

Now for any manually or locally written JSON , I would

1- Create a struct that confirms to Codable .

2- Create an instance of that Object .

3- Encode that object, and just convert it to String and it will 100% confirm to JSON verifiers .

Observe the code Below.

struct MyOject: Codable {
    var param1: String
    var param2: Int
    var param3: String
}

let myObj = MyOject(param1: "foo", param2: 1, param3: "bee")
let encodedData = try! JSONEncoder().encode(myObj) // encode the object as JSON Data
let myJSON = String(data:encodedData, encoding: .utf8)! // converting to String that is indeed JSON formatted
print(myJSON)

//Now to use it as object again we just need to Decode it. (the data)

let myObjResult = try! JSONDecoder().decode(MyOject.self, from: encodedData) // converted back as object
print(myObj.param1) // test reuslt should be (foo)

Now what are the benefits

1- The most important thing reusability you can reuse it as much as you need.

2- 100% valid JSON provider.

3- Gives you a big skill in the future to handle the Responses from any API.

4- This is by far the easiest way to do so and the fastest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM