Reputation: 107
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
Upvotes: 0
Views: 47
Reputation: 5088
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.
Upvotes: 1
Reputation: 1061
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.
Upvotes: 1