我已經將javascript從jQuery對象傳遞到swift。在swift中更改回原來的json結構
JS
'use strict';
var labdetail = function()
{
var labs = ["sdsdsd","suds","DH-sdsd","DH-sdsd"]
var labgrp = {labGrp:"R", labGrpDesc:"sdsd",labs:labs}
var labGroups = []
labGroups[0] = labgrp
var labs1 = ["dsdsd","suds","DH-sdsd","DH-dfdf]
var labgrp1 = {labGrp:"H", labGrpDesc:"dfdf",labs:labs}
labGroups[1] = labgrp1
return JSON.stringify(labGroups);
}
迅速
import UIKit
import JavaScriptCore
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let context = JSContext()
let returnLabResultJSPath = NSBundle.mainBundle().pathForResource("returnLabResult", ofType: "js")
let contentData = NSFileManager.defaultManager().contentsAtPath(returnLabResultJSPath!)
let content = NSString(data: contentData!, encoding: NSUTF8StringEncoding) as? String
// let returnresult = try String(contentsOfFile: returnLabResultJSPath!, encoding: NSUTF8StringEncoding)
context.evaluateScript(content)
let getlabfunction = context.objectForKeyedSubscript("labdetail")
var labresult = getlabfunction.callWithArguments([])
let labresultdict :dictionary = labresult.toDictionary()
print(labresult)
}
我想改回爲原來的結構。我嘗試使用todictionary()
但未能
現在實驗室結果的結果是 [{ 「labGrp」: 「R」, 「labGrpDesc」: 「悲悲」, 「實驗室」: 「悲悲」,「asdasd 」, 「sadsad-sadsa」, 「DH-asdasd」]},{ 「labGrp」: 「H」, 「labGrpDesc」: 「生物學」, 「實驗室」:[ 「asdsad」, 「asdsad」, 「和」, 「DH-asdasd」]}]
我希望它是一個數組,並且數組[0]包含{「labGrp」:「R」,「labGrpDesc」:「傷心難過」,「實驗室」:[悲傷的悲傷「,」asdasd「,」sadsad-sadsa「,」DH-asdasd「]}
and array [1] contains {」labGrp「:」H「,」labGrpDesc「:」Biology「,」labs 「:[」asdsad「,」asdsad「,」and「,」DH-asdasd「]}
你需要什麼真的.. **我想改變它恢復成原始結構**? –