[英]iOS Swift: How to change the Response from server to Specific Format?
I stored the data from server to NSArray, Here I want change the current format to new format. 我将数据从服务器存储到NSArray,在这里我想将当前格式更改为新格式。 But I don't know how to change the current format to new.Please help me for make the format. 但我不知道如何将当前格式更改为new。请帮助我制作格式。 Here I have share the old format and new format as following. 在这里,我共享了以下旧格式和新格式。
This is old format received from Server. 这是从服务器收到的旧格式。
{
{
“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”
}
} }
I want to change the new format like below. 我想更改如下的新格式。
{
{
“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对象映射。
The best design for what you are doing is to make models for your server response and then save those model objects in an array. 最好的设计是为服务器响应创建模型,然后将这些模型对象保存在数组中。 That way you can change what you want. 这样,您可以更改所需的内容。
Model for your old format: 旧格式的模型:
struct Item {
var aClass:String?
var section:String?
var name:String?
var mark:String?
var school:String?
var place:String?
}
Now models for your new updated server response: 现在为新的更新服务器响应建模:
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?
}
And then you can use it like: 然后您可以像这样使用它:
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])
I hope this solve your problem. 我希望这能解决您的问题。 If there is any confusion please ask. 如有任何疑问,请询问。 Good Luck :) 祝好运 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.