Any Method to remove comment characters from JSON response in Xcode? - iphone

I am new to JSON. Are there any methods in JSON parser to remove the comment characters from a response.
Eg. //{"response":"success".......
its SBJson for iPhone.
from http://code.google.com/p/json-framework

The JSON grammar doesn't allow comments. That doesn't answer your question obviously, but I suspect you'll have to do some string manipulation and replace all those comment characters with empty strings and parse it with the JSON library only after doing so.

Nowadays, very easy to do this:
Here's how you get and parse actual json
// normal json no comments
func getStuff() {
guard let url = URL(string: "http://you.com/x.json") else { return print("?") }
let configuration = URLSessionConfiguration.ephemeral
aSession = URLSession(configuration: configuration)
aSession.dataTask(with: url) { [weak self] data, response, error in
guard let _ = self else { return print("woe") }
guard let data = data else { return print("woe") }
do {
let result = try JSONDecoder().decode(YourStructure.self, from: data)
localBlah = Dictionary(uniqueKeysWithValues: result.whatever)
} catch let error {
print(error)
}
}.resume()
}
Here's how you get and parse "json" which has simple comment lines:
During development, remove #comment lines from "json":
Notice the line of code which decodes the data :
let result = try JSONDecoder().decode(YourStructure.self, from: data)
Simply paste in these three lines of code, before, that line:
let s = String(decoding: data, as: UTF8.self)
let fixed = s.replacingOccurrences(
of: "(?m)^#.*",
with: "",
options: .regularExpression)
guard let data2: Data = fixed.data(using: .utf8) else { return print("woe") }
let result = try JSONDecoder().decode(YourStructure.self, from: data2)
So during development in your "json" on your server, you can have things like ..
"measures": [{
"screen": "options",
"topMargin": 25,
#don 't change topmargin anyone
"leftMargin": 12,
#Note,
Steve prefers 13. But everyone
else prefers 12. "rightMargin": 20,
},
It's that simple.
Important note on using regex:
Regex is a sophisticated process. The example regex used in this post simply means
"Delete any full lines, which, start with a '#'
So, it only understands simple "full-line" comments.
How to write regex is beyond the scope of this QA.
When you pre-munge the text at let fixed =, use whatever regex or other technique you wish.

The JSON parsers are very finicky about what is at the start of a JSON block to parse - they DO NOT like characters other than "{" at the start (at least that's what I found with TouchJSON, and it sounds like your case with SBJson is similar).
So just take your string and eliminate any characters before the opening "{", then you can parse:
NSRange startJSONRange = [myJSONString rangeOfString:#"{"];
startJSONRange.length = myJSONString.length - startJSONRange.location;
NSString *correctJSONString = [myJSONString substringWithRange:startJSONRange];
// parse correctJSONString
That will work, but the REAL fix is to tell whoever is sending you JSON to cut out the nonsense and send real JSON.

Related

How to convert the email body to something readable?

Iam working on a simple Apple mailkit extension but cant get something readable out of my mails.
func allowMessageSendForSession(_ session: MEComposeSession, completion: #escaping (Error?) -> Void) {
let mailMessage = session.mailMessage;
let subject = mailMessage.subject
let sender = mailMessage.fromAddress.addressString ?? "undefined";
let data = String(data: mailMessage.rawData!, encoding: .utf8)
In data is the header and the mail body. But its filled with so many 'quoted-printable' strings.
Something like this Viele Gr=C3=BC=C3=\n=9Fe =F0=9F=A4=9D. It should be Viele Grüße 🤝.
I already tried the code in this answer https://stackoverflow.com/a/32827598/1407823 but it seems to only work with single words. I cannot get it to work with a whole text.
Is there no built in way to parse text like this?
There is no built-in way to decode the message, you need a RFC822 parser for example MimeParser on GitHub, available as Swift Package.
This is an example how to decode the body as plain text, messageData represents the raw data of the message
import MimeParser
do {
let messageString = String(data: messageData, encoding: .utf8)!
let parser = MimeParser()
let mime = try parser.parse(messageString)
switch mime.content {
case .body(let body): print(body.raw)
case .alternative(let mimes), .mixed(let mimes):
if let plainTextMime = mimes.first(where: {$0.header.contentType?.subtype == "plain"}),
let decodedBody = try plainTextMime.decodedContentString() {
print(decodedBody)
}
}
} catch {
print(error)
}
With subtype == "html" you get the HTML text, if available

How to edit and update xml file with Swift

I'm trying to merge two xml files to a new xml file. Also adding one line <xxxxxxxxxxXML Seperatorxxxxxxxxxx> in between two xml.
I'm using SWXMLHash pod to parser my xml file. I already have two var of xml that I need to merge. Thanks!
var oldXML: XMLIndexer?
var newXML: XMLIndexer?
var combinedXML: XMLIndexer?
func mergeEHR(oldXML: XMLIndexer, newXML: XMLIndexer) -> XMLIndexer {
do {
/// Don't know how to merge it.
} catch {
print(error)
}
}
I want it to be
var combinedXML =
<oldXML>
...
</oldXML>
xxxxxxxxxxXML Seperatorxxxxxxxxxx
<newXML>
...
</newXML>
Since combined XML will be not a valid XML (even if it was, doesn't matter), treat them as a raw String:
let oldXMLString = try! String(contentsOfFile: <#PathToOldXML#>) // If it is on the file, else assign it the way it should
let separator = "<xxxxxxxxxxXML Seperatorxxxxxxxxxx>"
let newXMLString = try! String(contentsOfFile: <#PathToNewXML#>) // If it is on the file, else assign it the way it should
let combinedXMLString = [oldXMLString, separator, newXMLString].joined(separator: "\n")
try! combinedXMLString.write(toFile: <#PathToDestinationOfTheCombinedXML#>, atomically: <#Bool#>, encoding: <#String.Encoding#>)
Then you can treat the result as an invalid XML or etc. again.

How can I write to a file, line by line in Swift 4

I need help figuring out how to write repeatedly to the same, open, output file.
I am using Swift 4.2. My searches and tests have turned up only code that writes a single text string to a file and then closes the file. The next opening overwrites the last one. An example is shown below.
The problem is that I need to be able to write large numbers of records (say, 1.5 million) and perform calculations on each record just before it is written to a file. That’s not feasible when the code will only write once before closing. I'm calling this "writing line by line", much like the opposite, to "read line by line."
I tried to find an option in various Swift write statements and SO posts, but everything seems to be geared toward writing once then closing the file. I tried an open for append, but that did not work and anyway it seems inefficient to open, close, reopen-append each time I want to write to a file. I tried some C code in Swift, using open(… and freopen(… but could not get something that the compiler wouldn't complain about. Hopefully, there is a way to do this all in Swift. The following code works nicely for one write.
let file0 = “test_file.txt”
let s0 = ("This is a test line of text")
do {
try s0.write(to: NSURL(fileURLWithPath: file0) as URL, atomically: false, encoding: String.Encoding.utf8)
} catch {
print("Problem writing to file0")
}
How can I adapt this code snippet to write a string, and then another and another etc, and before closing the file when it’s all done? If not with this, is there Swift code that will do the job?
Following are the essential code components needed to write to a file, line-by-line in Swift. First is some file management code to create a file if it does not exist, then there is code to print a series of example statements, followed by code to print to the file in a loop, and finally close the file. This code worked correctly in Swift 4.2. The difference between this and the method in the question is that the write statements in this code use a method of fileHandle! and the question shows a method of a Swift string.
print("Swift_Write_to_File_Test_1")
var outFilename: NSString = "test_file.txt"
// Begin file manager segment
// Check for file presence and create it if it does not exist
let filemgr = FileManager.default
let path = filemgr.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).last?.appendingPathComponent(outFilename as String)
if !filemgr.fileExists(atPath: (path?.absoluteString)!) {
filemgr.createFile(atPath: String(outFilename), contents:Data(" ".utf8), attributes: nil)
}
// End file manager Segment
// Open outFilename for writing – this does not create a file
let fileHandle = FileHandle(forWritingAtPath: outFilename as String)
if(fileHandle == nil)
{
print("Open of outFilename forWritingAtPath: failed. \nCheck whether the file already exists. \nIt should already exist.\n");
exit(0)
}
var str: NSString = "2. Test string from NSString.\n";
var str0: String = "3. Test string from a Swift String.\n"
var str1: NSString = "4. Test string from NSString.\n";
fileHandle!.write("1. Text String in-line with code statement.\n".data(using: .utf8)!)
fileHandle!.write(String(str).data(using: .utf8)!)
fileHandle!.write(str0.data(using: .utf8)!)
fileHandle!.write(String(str1).data(using: .utf8)!)
fileHandle!.write("5. Text String in-line with code statement.\n".data(using: .utf8)!)
fileHandle!.write("6. Text in a loop follows: \n".data(using: .utf8)!)
for i in 0...5
{
//Assemble a string then write it to the file.
var s0: String = ""
s0 = String(i)
//s0.append(" ... some text here.\n") // See improvement below
s0 += " ... some text here.\n" // This is a better than .append
fileHandle!.write(s0.data(using: .utf8)!)
}
// End of file-writing segment
fileHandle!.closeFile()
This worked for me in Swift 5:
func writeFile() -> Bool
{
let outFilename: String = "test_file.txt"
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let outFilePath = documentsURL!.appendingPathComponent(outFilename).path
let fileManager = FileManager.default
// If file exists, remove it
if fileManager.fileExists(atPath: outFilePath)
{
do { try fileManager.removeItem(atPath: outFilePath) }
catch { return false }
}
// Create file and open it for writing
fileManager.createFile(atPath: outFilePath, contents:Data(" ".utf8), attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: outFilePath)
if fileHandle == nil
{
return false
}
else
{
// Write data
fileHandle!.write("Test line 1\n".data(using: .utf8)!)
fileHandle!.write("Test line 2\n".data(using: .utf8)!)
fileHandle!.write("Test line 3\n".data(using: .utf8)!)
// Close file
fileHandle!.closeFile()
return true
}
}

Alamofire + swiftyJSON array parsing

When I got JSON {"categories": "[\"a/b/c\",\"d\"]"} from server,
Then How can I parse it to ["a/b/c", "d"]
When I using .arrayValue, then it always return []
You have an array represented as raw JSON string within your JSON. You have three options:
Try to manually scan through replacing occurrences of \" (which you'll have to confusingly escape both the backslash character and the quotation mark encountered with a backslash when you write your code, e.g. "\\\"") before you parse the JSON. To do this, you'd probably use responseData rather than responseJSON, do your manual replacement of characters, and then manually parse it yourself with NSJSONSerialization. The downside here is that you might want to check for other escaped characters, too (e.g. what if the nested JSON was pretty printed, then you might have \n or even \t in there, too, which you'd want to convert as well).
Manually run JSON parsing again for each of those values that contain a JSON string in the responseObject. If you're stuck with the existing JSON, I think this is probably safest.
For example, if the raw json was really just:
{"categories": "[\"a/b/c\",\"d\"]"}
Then you could do something like:
Alamofire.request(request)
.responseJSON { response in
guard response.result.error == nil else {
print(response.result.error!)
return
}
do {
if let dictionary = response.result.value as? [String: String],
let categoryData = dictionary["categories"]?.dataUsingEncoding(NSUTF8StringEncoding),
let categories = try NSJSONSerialization.JSONObjectWithData(categoryData, options: []) as? [String]
{
let result = ["categories" : categories]
print(result)
}
} catch {
print(error)
}
}
Then the result would be:
["categories": ["a/b/c", "d"]]
Fix the web service that's generating this JSON so that it's not doing this silliness. This is probably the best solution, though it will take some detective work to figure out why/how the web service put JSON inside JSON. I know that you say you can't change the server side, but you really should escalate this to someone on the server team to fix this. It's silly to write client code to work around some mistake (either a mistake in the server code itself, or how the server was provided the data in the first place).
First up, the response you're getting from the server is a String and not Array. Hence you'll get empty array when you do .arrayValue . If you want to convert the string into an array, first you need to remove the "[" & "]" and then you need to do the componentsSeparatedByString with string as "," for the resultant string. You can use the following code snippet to do that:
let str = "[\"a/b/c\",\"d\"]"
let filteredString = String (str.characters.filter { $0 != "[" && $0 != "]"})
print(filteredString)
let filteredStringArray = filteredString.componentsSeparatedByString(",")
print(filteredStringArray)
HTH :)

xcode error 'consecutive statements on a line must be separated by' when creating endpoint URL for REST API datatask

I am a beginner, trying to code a POST datarequest to post a vote to the 'rating' field of a Drupalnode (so that users can rate movies). I have followed online guides, carefully copying the syntax, but in Xcode am receiving this error for for this line:
let movieEndpoint: String = https://www.examplesitename.com/film1
The red error message is "consecutive statements on a line must be separated by a ';'
The error highlights the ':' after https, and suggests "fix it" with an ';' but changing it to https;www.examplesitename.com/film1 then brings up another red error 'expected expression' (and doesn't seem correct as it is a URL)
For context, below is my code, (which I hope will work to post my data request but haven't been able to check yet)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let movieEndpoint: String = https://www.sitename.com/film1
guard let movieURL = NSURL(string: movieEndpoint) else {
print("Error: cannot create URL")
return
}
let movieUrlRequest = NSMutableURLRequest(URL: movieURL)
movieUrlRequest.HTTPMethod = "POST"
let task = session.dataTaskWithRequest(movieUrlRequest, completionHandler:{ _, _, _ in })
let newRating = ["rating": 50, "userId": 1,]
let jsonRating: NSData
do {
jsonRating = try NSJSONSerialization.dataWithJSONObject(newRating, options: [])
movieUrlRequest.HTTPBody = jsonRating
} catch {
print("Error: cannot create JSON from todo")
return
}
movieUrlRequest.HTTPBody = jsonRating
task.resume()
}
Thank you for any help you can give me.
The proper way to declare a String in Swift is to add " " around the string.
Fix your code like this:
let movieEndpoint: String = "https://www.sitename.com/film1"