0

Here I am getting a number from web service which I had stroed it in userdefaults and retrieving from user defaults and in another class and converting from json string to string to use it but unable to implement it and it got crashed at first line can anyone help help me how to resolve it ?

here is my code for retrieving

let customerId = UserDefaults.standard.value(forKey: "CustomerLoginNumber")
            jsonToString(json: customerId as! String)
            print("\(UserDefaults.standard.value(forKey: "CustomerLoginNumber")!)")
 func jsonToString(json: String){
        do
        {
            let data1 =  try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // first of all convert json to the data
            let convertedString = String(data: data1, encoding: String.Encoding.utf8) // the data will be converted to the string
            print(convertedString ?? "defaultvalue")
            self.customerCartId = convertedString
        } catch let myJSONError {
            print(myJSONError)
        }
    }

and here is my error showing in line

2017-10-23 09:53:26.896 Ecommerce[966:20945] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'

  • Why do you convert a `String` to (JSON) `String`? It seems that the step is not needed at all. And **never** use `valueForKey` with `UserDefaults`. There is a method `stringForKey` – or `objectForKey` for unspecified objects. – vadian Oct 23 '17 at 04:43
  • i got a response form server that "210" if i save it in user defaults and when try to retrieve to post the got number to another json it is taking as "\"210\"" and it is wrongly posting so getting error @vadian –  Oct 23 '17 at 04:47
  • Then write `"\""+customerId+"\""`. `JSONSerialization` for a single string is overkill (and `prettyprinted` is nonsensical anyway). The exception occurs because the object is not a collection type. – vadian Oct 23 '17 at 04:52
  • but still adding more as shown here ["cartItem": ["qty": "1", "sku": "24-WG03", "quote_id": "\"\"210\"\""]] @vadian –  Oct 23 '17 at 04:56
  • @user JSON top level object should to be a dictionary or an array – Leo Dabus Oct 23 '17 at 04:58
  • In this structure the additional double quotes are most likely wrong. And **this** structure won't cause the exception. – vadian Oct 23 '17 at 04:59
  • then how to remove slashes for the number which i got from response @LeoDabus –  Oct 23 '17 at 05:03
  • You don't need to. Can you post the link so I can check what type of data you are getting? – Leo Dabus Oct 23 '17 at 05:04
  • And don't use UserDefaults to store data. Write the data to the App preferences folder located at your App's Library directory https://stackoverflow.com/questions/34701630/valid-file-path-for-archiverootobject-and-unarchiverootobject/34701970#34701970 – Leo Dabus Oct 23 '17 at 05:07
  • that is local server and the response is "210" @LeoDabus –  Oct 23 '17 at 05:08
  • how to write data to App's library directory ? @LeoDabus –  Oct 23 '17 at 05:09
  • I posted the link to it in the comment above – Leo Dabus Oct 23 '17 at 05:09
  • ok i have seen and how to get back the data again when in need ? @LeoDabus –  Oct 23 '17 at 05:10
  • Use Data(contentsOF: URL) to load it and to write just use Data write method. You can also use String(contentsOf: URL, encoding: .utf8) if the data is a simple text – Leo Dabus Oct 23 '17 at 05:10
  • https://stackoverflow.com/questions/24097826/read-and-write-data-from-text-file/26557965?s=2|57.3728#26557965 – Leo Dabus Oct 23 '17 at 05:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157251/discussion-between-user-and-leo-dabus). –  Oct 23 '17 at 05:20
  • url path property used to be optional but it is not anymore. Just remove the if keyword. But you don’t need the path just use the url. – Leo Dabus Oct 23 '17 at 12:55

0 Answers0