so I'm learning how to use JSON by doing a Blogger Reader App. I want to show in a table the image that it's inside of the blog entry, so when I download the code I have something like this:
所以我正在学习如何使用Blogger Reader App来使用JSON。我想在表格中显示它在博客条目中的图像,所以当我下载代码时,我有这样的东西:
"content = "<div style=\"font-family: 'Open Sans', Arial, sans-serif; font-size: 14px; margin-bottom: 15px; padding: 0px; text-align: justify;\">\nLorem ipsum dolor sit amet, Donec posuere nibh egestas fermentum consequat.</div>\n<div style=\"font-family: 'Open Sans', Arial, sans-serif; font-size: 14px; margin-bottom: 15px; padding: 0px; text-align: justify;\">\n<div class=\"separator\" style=\"clear: both; text-align: center;\">\n<a href=\"https://1.bp.blogspot.com/-HrIjp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."
And I need the address of the image, so I tried to simulate in playground by using this code:
我需要图像的地址,所以我尝试使用以下代码在游乐场中进行模拟:
let string = "<image is at: href=\u{5C}\u{22}https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."
let stringInit = string
let stringOperation = NSString(string: stringInit)
let stringArray = stringOperation.components(separatedBy: "href=\u{5C}\u{22}")
let intermediateTwo = stringArray[1]
let operationTwo = NSString(string: intermediateTwo)
let strinArrayTwo = operationTwo.components(separatedBy: ".png")
let image = strinArrayTwo[0] + ".png"
And it worked fine, but when I tried to use it in my app, it crashed because stringArray
had just one element. I changed this line:
它工作正常,但当我尝试在我的应用程序中使用它时,它崩溃了因为stringArray只有一个元素。我更改了这一行:
let stringArray = stringOperation.components(separatedBy: "href=\u{5C}\u{22}")
to:
至:
let stringArray = stringOperation.components(separatedBy: "href=")
And it works properly, I guess it worked in PlayGround because I had to enter the data the same way I'm looking for it, but I really don't know what else to try, so my question is, is there any way to make it work properly?
并且它工作正常,我猜它在PlayGround中工作,因为我必须以我正在寻找的方式输入数据,但我真的不知道还有什么可以尝试,所以我的问题是,有什么方法可以让它正常工作?
Thanks
谢谢
1 个解决方案
#1
1
I'm not sure how you detected your content
, if it's taken from JSON text directly, the head should be "content":
, not "content =
.
我不确定你是如何检测到你的内容的,如果它是直接从JSON文本中获取的,那么头部应该是“内容”:而不是“content =。
But anyway, it's actual content seems to be represented in a escaped format. (JSON text uses such format to represent JSON string, as well as debug output of Swift String.)
但无论如何,它的实际内容似乎以转义格式表示。 (JSON文本使用这种格式来表示JSON字符串,以及Swift String的调试输出。)
And when such escaped format representation is read into Swift String, some escaping sequence such as \"
is read as a single character "
.
当这种转义格式表示被读入Swift String时,一些转义序列如\“被读作单个字符”。
So, when you replace some part of the String into unicode escaped representation, you need to replace \"
into \u{22}
, not \u{5c}\u{22}
.
因此,当您将String的某些部分替换为unicode转义表示时,您需要将\“替换为\ u {22},而不是\ u {5c} \ u {22}。
Also you should always check the result of components(separatedBy:)
to avoid crashing with unexpected result.
此外,您应该始终检查组件的结果(separatedBy :)以避免意外结果崩溃。
So, your code should be something like this:
所以,你的代码应该是这样的:
let string = "<image is at: href=\u{22}https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."
let stringArray = string.components(separatedBy: "href=\u{22}")
if stringArray.count > 1 {
let intermediateTwo = stringArray[1]
let strinArrayTwo = intermediateTwo.components(separatedBy: ".png")
if strinArrayTwo.count > 1 {
let image = strinArrayTwo[0] + ".png"
} else {
print("'.png' not found")
}
} else {
print("'href=' not found")
}
But you should better consider using regular expression to extract substrings with this sort of pattern.
但是你最好考虑使用正则表达式来提取这种模式的子串。
let pattern = "href=\"(.*?\\.png)\""
let regex = try! NSRegularExpression(pattern: pattern)
if let match = regex.firstMatch(in: string, options: [], range: NSRange(0..<string.utf16.count)) {
let image = (string as NSString).substring(with: match.rangeAt(1))
print(image) //->https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png
} else {
print("'href=...png' not found")
}
#1
1
I'm not sure how you detected your content
, if it's taken from JSON text directly, the head should be "content":
, not "content =
.
我不确定你是如何检测到你的内容的,如果它是直接从JSON文本中获取的,那么头部应该是“内容”:而不是“content =。
But anyway, it's actual content seems to be represented in a escaped format. (JSON text uses such format to represent JSON string, as well as debug output of Swift String.)
但无论如何,它的实际内容似乎以转义格式表示。 (JSON文本使用这种格式来表示JSON字符串,以及Swift String的调试输出。)
And when such escaped format representation is read into Swift String, some escaping sequence such as \"
is read as a single character "
.
当这种转义格式表示被读入Swift String时,一些转义序列如\“被读作单个字符”。
So, when you replace some part of the String into unicode escaped representation, you need to replace \"
into \u{22}
, not \u{5c}\u{22}
.
因此,当您将String的某些部分替换为unicode转义表示时,您需要将\“替换为\ u {22},而不是\ u {5c} \ u {22}。
Also you should always check the result of components(separatedBy:)
to avoid crashing with unexpected result.
此外,您应该始终检查组件的结果(separatedBy :)以避免意外结果崩溃。
So, your code should be something like this:
所以,你的代码应该是这样的:
let string = "<image is at: href=\u{22}https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" imageanchor=\"1\" style=\"margin-left: 1em; margin-right: 1em;\"><img border=\"0\" data-original-height=\"1179\" data-original-width=\"1600\" height=\"235\" src=\"https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s320/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png\" width=\"320\" /></a></div>\n<br />\n<br />\nAliquam sed neque sit amet dolor sagittis maximus."
let stringArray = string.components(separatedBy: "href=\u{22}")
if stringArray.count > 1 {
let intermediateTwo = stringArray[1]
let strinArrayTwo = intermediateTwo.components(separatedBy: ".png")
if strinArrayTwo.count > 1 {
let image = strinArrayTwo[0] + ".png"
} else {
print("'.png' not found")
}
} else {
print("'href=' not found")
}
But you should better consider using regular expression to extract substrings with this sort of pattern.
但是你最好考虑使用正则表达式来提取这种模式的子串。
let pattern = "href=\"(.*?\\.png)\""
let regex = try! NSRegularExpression(pattern: pattern)
if let match = regex.firstMatch(in: string, options: [], range: NSRange(0..<string.utf16.count)) {
let image = (string as NSString).substring(with: match.rangeAt(1))
print(image) //->https://1.bp.blogspot.com/-HrI-jp5LBMk/WY5LjP7uiHI/AAAAAAAAAAo/54UtLmw3_oQFmqyhBgfLO9VUV5hSDIBlQCLcBGAs/s1600/Captura%2Bde%2Bpantalla%2B2017-08-11%2Ba%2Bla%2528s%2529%2B19.26.54.png
} else {
print("'href=...png' not found")
}