簡體   English   中英

如何快速編寫正確的JSON字符串文件

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

我創建了一個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

正確的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"
      }
   ]
}

您的錯誤沒有整個數組的鍵,請在matchIds的結尾處使用印刷術引號和多余的右花括號。

我想學習,以便將來自己能做

好吧,要寫出有效的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-到目前為止,這是最簡單,最快的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM