[英]How to show new line with numbers format from server response of String in iOS
[英]iOS Swift: How to change the Response from server to Specific Format?
我將數據從服務器存儲到NSArray,在這里我想將當前格式更改為新格式。 但我不知道如何將當前格式更改為new。請幫助我制作格式。 在這里,我共享了以下舊格式和新格式。
這是從服務器收到的舊格式。
{
{
“class” = “12”,
“section” = “A”,
“name” = “aathi”,
“mark” = “850”,
“school” = “ab matriculation school”,
“place” = “Chennai”
},
{
“class” = “12”,
“section” = “B”,
“name” = “ram”,
“mark” = “904”,
“school” = “ab matriculation school”,
“place” = “delhi”
},
{
“class” = “10”,
“section” = “C”,
“name” = “ashok”,
“mark” = “389”,
“school” = “psg hr sec school”,
“place” = “coimbatore”
},
{
“class” = “8”,
“section” = “B”,
“name” = “sundar”,
“mark” = “267”,
“school” = “govt hr sec school”,
“place” = “Madurai”
}
}
我想更改如下的新格式。
{
{
“class” = “12”,
“section” = “A”,
“school” = “ab matriculation school”,
noncommondetails:{
{
“name” = “aathi”,
“mark” = “850”,
“place” = “Chennai”
},
{
“name” = “ram”,
“mark” = “904”,
“place” = “delhi”
}
}
},
{
“class” = “10”,
“section” = “C”,
“school” = “psg hr sec school”,
noncommondetails:{
{
“name” = “ashok”,
“mark” = “389”,
“place” = “coimbatore”
}
},
},
{
“class” = “8”,
“section” = “B”,
“school” = “govt hr sec school”,
noncommondetails:{
{
“name” = “sundar”,
“mark” = “267”,
“place” = “Madurai”
},
}
}
}
您必須嘗試https://github.com/Hearst-DD/ObjectMapper進行json對象映射。
最好的設計是為服務器響應創建模型,然后將這些模型對象保存在數組中。 這樣,您可以更改所需的內容。
舊格式的模型:
struct Item {
var aClass:String?
var section:String?
var name:String?
var mark:String?
var school:String?
var place:String?
}
現在為新的更新服務器響應建模:
struct Item {
var aClass:String?
var section:String?
var school:String?
var nonCommonDetails : [NonCommenItem]?
}
struct NonCommenItem {
var name:String?
var mark:String?
var place:String?
}
然后您可以像這樣使用它:
let nonCommenItem1 = NonCommenItem(name: "aathi", mark: "850", place: "Chennai")
let nonCommenItem2 = NonCommenItem(name: "ram", mark: "904", place: "delhi")
let item1 = Item(aClass: "12", section: "B", school: "ab matriculation school", nonCommonDetails: [nonCommenItem1,nonCommenItem2])
我希望這能解決您的問題。 如有任何疑問,請詢問。 祝好運 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.