Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 解码字符串中转义的JSON对象_Ios_Json_Swift_Decode_Codable - Fatal编程技术网

Ios 解码字符串中转义的JSON对象

Ios 解码字符串中转义的JSON对象,ios,json,swift,decode,codable,Ios,Json,Swift,Decode,Codable,我从一个API(不幸的是,我无法更改它)得到的响应看起来像(只是一个示例): As bytes=>“{\“key\”:\“value\”}” 起始引号、结束引号和转义引号都是响应的一部分,我正在以一种非常丑陋的方式解决它,看起来像这样: // (...) Receiving response guard var responseString = String(bytes: data, encoding: .utf8) else { print("Response wasn't just

我从一个API(不幸的是,我无法更改它)得到的响应看起来像(只是一个示例):

As bytes=>
“{\“key\”:\“value\”}”

起始引号、结束引号和转义引号都是响应的一部分,我正在以一种非常丑陋的方式解决它,看起来像这样:

// (...) Receiving response

guard var responseString = String(bytes: data, encoding: .utf8) else {
    print("Response wasn't just a string, great!") // unfortunately, this never happens
    return
}
responseString = responseString.trimmingCharacters(in: .whitespacesAndNewlines) // make sure it is trimmed
responseString = String(responseString.dropFirst()) // drop the quote at the start
responseString = String(responseString.dropLast()) // drop the quote at the end
responseString = responseString.replacingOccurrences(of: "\\\"", with: "\"")// convert all \" to " (and hope nothing else is escaped <<< this is really bad!!!)
let responseDataToDecode = responseString.data(using: .utf8)!

// (...) decoding with JSONDecoder
let str = #""{\"key\":\"value\"}""#
// "{\\"key\\":\\"value\\"}"
/(…)接收响应
guard var responseString=String(字节:数据,编码:.utf8)else{
print(“响应不仅仅是一个字符串,太棒了!”)//不幸的是,这种情况从未发生过
返回
}
responseString=responseString.trimmingCharacters(in:.whitespacesAndNewlines)//确保已修剪
responseString=String(responseString.dropFirst())//在开始处删除引号
responseString=String(responseString.dropLast())//删除结尾的引号

responseString=responseString.replacingOccurrences(of:“\\\”,with:“\”)//将所有\“转换为”(希望别漏掉任何东西第一步是:你需要一份正式的书面声明,说明你的数据的确切格式。看起来好像有人拿了一些数据,将其转换为JSON数据,将数据解释为字符串,然后将字符串转换为JSON片段。解码JSON片段,得到一个s并不困难tring,将字符串转换为数据,并将该数据解码为JSON(从JSONSerialization和.allowFragments开始,这可能是您一生中唯一应该使用.allowFragments的时候)。不咒骂就很难做到这一点


但首先你要写的是,这实际上是一种格式。因为我敢打赌,负责该数据格式的人最终会更改它,而不会告诉你并破坏你的代码。

如果它是双重编码的,那么你只需要双重解码。如果我理解正确,传入的数据如下所示:

// (...) Receiving response

guard var responseString = String(bytes: data, encoding: .utf8) else {
    print("Response wasn't just a string, great!") // unfortunately, this never happens
    return
}
responseString = responseString.trimmingCharacters(in: .whitespacesAndNewlines) // make sure it is trimmed
responseString = String(responseString.dropFirst()) // drop the quote at the start
responseString = String(responseString.dropLast()) // drop the quote at the end
responseString = responseString.replacingOccurrences(of: "\\\"", with: "\"")// convert all \" to " (and hope nothing else is escaped <<< this is really bad!!!)
let responseDataToDecode = responseString.data(using: .utf8)!

// (...) decoding with JSONDecoder
let str = #""{\"key\":\"value\"}""#
// "{\\"key\\":\\"value\\"}"
第一个字节是
,第二个字节是
{
,第三个字节是
\
,第四个字节是

这是一个JSON编码的字符串。所以将其解码为一个字符串(曾经因为它是一个“片段”而不起作用,但它目前运行良好,至少在我的所有测试中是如此):

然后将其解码为您的类型(
[String:String]
仅举个例子):


(顺便说一句,在我看来,这种双重编码很好,我不知道为什么会有这么多反对它的评论。在许多情况下,序列化任意对象比强制模式处理任意结构更有意义。只要它是干净编码的,我看不出有任何问题。)

这是一个有效的JSON字符串哦,它是一个JSON编码的两次!哦,我的天啊。请以NSLog显示的字符串形式向我们展示实际数据。给我们一些真实的数据让我们大开眼界。:)JSON与JSON?使用JSON序列化并使用
。allowFragments
,然后将其转换回数据,然后使用JSONDecoder()好吧,这很奇怪。否则也给制造这个双重JSON的罪犯一个很好的打击。有一段时间,这不起作用,因为它是一个“碎片”,我认为那是iOS 12和之前的时间,尽管我可能错了。