简体   繁体   English

iOS Swift:如何将响应从服务器更改为特定格式?

[英]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.

相关问题 如何从iOS中的String服务器响应显示数字格式的新行 - How to show new line with numbers format from server response of String in iOS swift-如何从服务器获取JSON响应 - swift - how to get JSON response from server 如何快速接收服务器的响应? - How to recieve response from server in swift? Swift:如何从 json 响应 iOS 创建动态布局 - Swift :How to create dynamic layout from json response iOS iOS Swift 如何从 json 响应中获取任何键的值 - iOS Swift How to get value of any key from json response 如何从推送通知响应 Swift 3/iOS 中获取数据 - How to get data from push notification response Swift 3/iOS 如何显示iclouds中的特定文件格式源自iOS Swift - How to show particular file format from iclouds derive in ios swift 如何快速从UItextfield中读取特定格式(XXX.X)? - How to read a specific format ( XXX.X ) from UItextfield in swift? 如何在Swift 3中将来自服务器的响应从字符串转换为数组? - How to convert the response got from a server from string to array in swift 3? 如何在服务器上发布帖子,等待响应,然后根据响应在Swift中更改视图? - How to make a post to a server, wait on the response, then change the view based on response in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM