0

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

6
  • what is the question ? and what are you trying to achieve ? Commented Nov 13, 2018 at 7:49
  • i dont have API so i am trying to build a JSON file inside my project so that i can parse data from that file , but i dont konw how to write JSON in correct format Commented Nov 13, 2018 at 7:51
  • i could help you write one, but not understand one i will leave an article to explain json are you interested in that answer or just a corret syntax ? Commented Nov 13, 2018 at 7:52
  • i want to learn so that i can do it by myself in future @Tobi Commented Nov 13, 2018 at 7:56
  • check out my latest answer. would help you Commented Nov 13, 2018 at 8:06

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

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.