From 83a7db9d6402e6560c714feb86e5583d0da06373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E5=B7=8D?= Date: Tue, 30 Nov 2021 14:27:42 +0800 Subject: [PATCH] fix bug --- .../Classes/Builder/JSONBuilder.swift | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/JSONConverter/Classes/Builder/JSONBuilder.swift b/JSONConverter/Classes/Builder/JSONBuilder.swift index 225464e..8fd65e1 100644 --- a/JSONConverter/Classes/Builder/JSONBuilder.swift +++ b/JSONConverter/Classes/Builder/JSONBuilder.swift @@ -72,13 +72,7 @@ class JSONBuilder { } private func addArraryWithParentNodeName(_ parentNodeName: String, keyName: String, valueArrary: [Any]) -> Property? { - var item = valueArrary.first - if valueArrary.first is Dictionary { - var temp = [String: Any]() - valueArrary.forEach { temp.merge($0 as! [String: Any]) { $1 } } - item = temp - } - + let item = valueArrary.first is Dictionary ? buildPrefectDictionary(arrary: valueArrary as! [[String : Any]]) : valueArrary.first if let item = item { var propertyModel: Property? switch item { @@ -103,6 +97,27 @@ class JSONBuilder { return nil } } + + private func buildPrefectDictionary(arrary: [[String: Any]]) -> [String: Any] { + var temp = [String: Any]() + arrary.forEach { + temp.merge($0) { (current, new) in + if current is NSNull { + return new + } else if ((current as? NSNumber)?.valueType()) == .Int { + if let newNumType = (new as? NSNumber)?.valueType() { + return (newNumType == .Float || newNumType == .Double) ? new : current + } else { + return new is NSNull ? current : new + } + } else { + return current + } + } + } + + return temp + } }