Php 将Json数据转换为用户可读的swift格式
我从PHPAPI获得了这个json数据。但如何将其转换为在应用程序中显示。实际上,在添加圈名称和id之前,它可以正常工作,但是当这个api返回圈名称时,它就不工作了。请帮忙。提前谢谢Php 将Json数据转换为用户可读的swift格式,php,arrays,json,swift,dictionary,Php,Arrays,Json,Swift,Dictionary,我从PHPAPI获得了这个json数据。但如何将其转换为在应用程序中显示。实际上,在添加圈名称和id之前,它可以正常工作,但是当这个api返回圈名称时,它就不工作了。请帮忙。提前谢谢 JSON:["data": <__NSArrayM 0x60800025bed0>( { Email = "ganesh@gmail.com"; ID = 104; Name = archana; Phone = "( 91)9111111110"; status
JSON:["data": <__NSArrayM 0x60800025bed0>(
{
Email = "ganesh@gmail.com";
ID = 104;
Name = archana;
Phone = "( 91)9111111110";
status = 1;
timeinterval = 15;
userpic = "";
},
{
circleName = "<null>";
circleid = 155;
}
)
]
您有一个字典数组,每个字典在解析时都有不同的结构 我为什么这么说
json = [dictionaryOne, dictionaryTwo...]
dictionaryOne = ["Email": "" , "ID": ....]
dictionaryTwo = ["cicileName": nil, "circleid": ""....]
现在在分析数组时
for UserDic in UserArray {
// here initialising the variables
}
您不能确保第二个结构字典会出现,这就是为什么会出现错误
简单的解决办法是进行检查,即
for UserDic in UserArray {
if let circleName = (UserDic as AnyObject).object(forKey: "circleid") as? String {
// do not parse if you don't need the data of this dict and continue
continue;
} else {
// parse the other dictionary which has Email, ID etc
var Email: String
var ID:String
.
.
.
}
}
如果要同时存储这两个字典,请定义类似的变量并存储在If块中。我的代码中有错误吗@Krisrootesee
circleName=“”代码>。这意味着您的circleName
是来自json的nil
。如果circleName=“Family”,那么解决方案是什么@RAJAMOHAN SifcircleName=“Family”
您遇到了什么错误?如果circleName=“Family”,那么错误是什么solution@ArchanaSIngh我已经为您提供了解决方案。您在两个不同的字典上循环,每个字典都有不同的结构,因此要解析它们,您需要两组逻辑。如果让circleid=((UserDic作为AnyObject)。object(forKey:“circleid”)作为?字符串,我没有得到我使用的结果!错误是“条件绑定的初始值设定项必须具有可选类型,而不是‘String’”,因为@archnasingh此行不是可选的((UserDic作为AnyObject)。object(forKey:“circleid”)作为?String)代码>。您可以使用此行(UserDic as?[String:AnyObject])。object(forKey:“circleid”)as?String
我应该在哪里写这行
for UserDic in UserArray {
if let circleName = (UserDic as AnyObject).object(forKey: "circleid") as? String {
// do not parse if you don't need the data of this dict and continue
continue;
} else {
// parse the other dictionary which has Email, ID etc
var Email: String
var ID:String
.
.
.
}
}