0

I am wondering how can I pass object from Javascript to swift. I don't know which type should i declare the variable in order to receive the javascript object.

the following is my Javascript code to create an object

var labs = ["dead","sfdsf","DH-dsfdsfds","DH-sdfdsf"]
var labgrp = {labGrp:"R", labGrpDesc:"fdgfdg",labs:labs}
var labGroups = []
labGroups[0] = labgrp
var labGroup = {labGroups}

how can I pass labGroup to swift? By using JSON?? Please comment it. thank you.

N J
  • 27,217
  • 13
  • 76
  • 96
raymond
  • 3
  • 6

1 Answers1

0

how can i pass labGroup to swift? By using JSON??

YES

Best option to you is JSON .

Pass JSON from Javascript and catch it in Swift


EDIT

see this for converting Javascript object to JSON

var j={"name":"binchen"};
JSON.stringify(j);

Note: download js for above code to work path

Community
  • 1
  • 1
N J
  • 27,217
  • 13
  • 76
  • 96
  • how can i convert labGroup object to JSON in javascript?? i am new in programming. thank you – raymond May 31 '16 at 10:01
  • i have raised other question for receiving the json from javascript http://stackoverflow.com/questions/37558381/swift-step-to-receive-json-object-from-javascript could you please kindly help? – raymond Jun 01 '16 at 03:24
  • And also, how to handle it after passing to swift – raymond Jun 01 '16 at 04:21
  • see this link http://stackoverflow.com/questions/24013410/how-to-parse-a-json-file-in-swift – N J Jun 01 '16 at 04:41
  • the example is showing how to handle the json. however, after stringify, it becomes jsvalue in swift – raymond Jun 01 '16 at 06:35
  • i try to use todictionary() but fail – raymond Jun 01 '16 at 07:02