![](/img/trans.png)
[英]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.