title: NSString from webview with JSON string
body: <p>I have a webview from which I'm calling a method that returns result of <code>JSON.stringify();</code>
  Then it is stored in database and used later to restore state of the page in webview.</p>
  
  <pre><code>NSString *data = [self.browser stringByEvaluatingJavaScriptFromString:@"course.serialize();"];
  [self.course storeRawSerializedData:data];
  </code></pre>
  
  <p>when it is retrieved from database (Core Data) I'm injecting it into loaded html page source like so:</p>
  
  <pre><code>-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
      if(_data){
          NSString* content = [[NSString alloc] initWithData:_data
                                                    encoding:NSUTF8StringEncoding];
          _data = nil;
          _jsReady = YES;
  
          NSString *data = @"";
          if(self.course.returnRawSerializedData){
              data = [data stringByAppendingString:@"&lt;script type=\"text/javascript\"&gt;var restoreData=%@;&lt;/script&gt;"];
              data = [NSString stringWithFormat:data, self.course.returnRawSerializedData];
          }
          //inject extra content to the page
          content = [data stringByAppendingString:content];
          //load modified HTML string
          [self.browser loadHTMLString:content baseURL:_url];
      }
  }
  </code></pre>
  
  <p>The problem is with JSON being invalid after this trip:</p>
  
  <p>the json returned by webview is:</p>
  
  <p><code>{"status":"incomplete","data_json":"{\"colour\":\"#000000\"}"}</code></p>
  
  <p>and after injecting it looks like this:</p>
  
  <p><code>&lt;script type=\"text/javascript\"&gt;var restoreData={"status":"incomplete","data_json":"{\"colour\":\"#000000\"}"};&lt;/script&gt;</code></p>
  
  <p>The page returns the <code>SyntaxError: JSON parse error: Unexpected identifier "object"</code></p>
  
  <p>When I wrap it with single or double quotes in stringByAppendingString method then page returns the <code>SyntaxError: JSON parse error: Expected '}'</code></p>
  
  <p>It works when I escape all double quotes (manually) but I don;t know how to do it programatically, I expected that string returned from stringByEvaluatingJavaScriptFromString will be already escaped to be used within double quotes.</p>