简体   繁体   English

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

[英]how to write correct JSON string file in swift

i have created a JSON string file which contains 我创建了一个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",
 }
 ] }

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 但是当我在在线Json Reader上检查此JSON时,它显示了很多错误,我是JSON解析的新手,不知道如何更正JSON

Correct JSON syntax: 正确的JSON语法:

{
   "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. 您的错误没有整个数组的键,请在matchIds的结尾处使用印刷术引号和多余的右花括号。

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 , 好吧,要写出有效的JSON 100%而没有任何大问题,我建议Codable

Now for any manually or locally written JSON , I would 现在,对于任何手动或本地编写的JSON ,我将

1- Create a struct that confirms to Codable . 1-创建一个结构以确认Codable

2- Create an instance of that Object . 2-创建该Object的实例。

3- Encode that object, and just convert it to String and it will 100% confirm to JSON verifiers . 3-对该对象进行Encode ,然后将其转换为String ,它将100%确认为JSON验证程序。

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. 1-最重要的事情是可重用性,您可以根据需要进行多次重用。

2- 100% valid JSON provider. 2-100%有效的JSON提供程序。

3- Gives you a big skill in the future to handle the Responses from any API. 3-为您提供将来处理任何API Responses技能。

4- This is by far the easiest way to do so and the fastest. 4-到目前为止,这是最简单,最快的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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