开发者

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

开发者 https://www.devze.com 2022-12-18 13:35 出处:网络
I am new to JSON. Are there any methods in J开发者_运维知识库SON parser to remove the comment characters from a response.

I am new to JSON. Are there any methods in J开发者_运维知识库SON 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消