如何快速编写正确的JSON字符串文件

时间:2018-11-13 07:45:37

标签: json

我创建了一个包含

的JSON字符串文件
{
[{"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",
 }
 ] }

但是当我在在线Json Reader上检查此JSON时,它显示了许多错误,并且我是JSON解析的新手,不知道如何纠正JSON

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:1)

  

我想学习,以便将来自己能自己做

嗯,要写出有效的JSON 100%且没有大问题,我建议Codable

现在,对于任何手动或本地编写的JSON,我都会

1-创建一个确认为Codable的结构。

2-创建该Object的实例。

3- Encode该对象,然后将其转换为String,它将100%确认给JSON验证者。

观察下面的代码。

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)

现在有什么好处

1-最重要的是可重用性,您可以根据需要重复使用它。

2-100%有效的JSON提供者。

3-在将来为您提供一项重要技能,使您可以通过任何API处理Responses

4-到目前为止,这是最简单的方法,也是最快的方法。