iOS 5 JSON Parsing Results in Cocoa Error 3840
One thing that strikes me as incorrect is this:
[[NSBundle mainBundle] pathForResource:@"Locations-JSON" ofType:@"rtf"]
Your data is an RTF file?? It should be a txt
file (or any other sort of plain text file). RTF files usually contain text formatting data, like this:
{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf470
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
\f0\fs24 \cf0 \{"States": [\{"Name": "Arizona","Cities": [\{"Name": "Phoenix"\}]\},\{"Name": "California","Cities": [\{"Name": "Orange County"\},\{"Name": "Riverside"\},\{"Name": "San Diego"\},\{"Name": "San Francisco"\}]\},\{"Name": "Nevada","Cities": [\{"Name": "Las Vegas"\}]\}]\}}
When I read that in as a data and try to parse it as JSON, I get the 3840 error you're seeing. That error's description says:
The data couldn’t be read because it has been corrupted. (No string key for value in object around character 2.)
So what it looks like to me is that you don't actually have JSON. You have RTF data.
error parsing JSON response Cocoa error 3840
Try removing
Content-Length
Accept
and
Content-Type
from your code. It may help you.
I had the same issue with json WCF, though my json object was valid json.
I removed all those 3 from code and it worked. The server was not accepting those.
I dont know why..
Cheers
Inconsistent JSON Parsing in XCODE (Cocoa error 3840)
Your second JSON has a string, /* School Info */
, after the value associated with the "@xmlns:sql"
key. I don't know how that got in there (it makes no sense to have a C-style comment in the middle of a JSON), but if you take that out, you should be good.
If you use a tool like http://jsonlint.com, it can help to identify these sorts of issues.
json parsing failed - cocoa error 3840 and invalid value around character 0
Updated answer:
I think now that you have posted the Objective-C code that uses this received JSON, it becomes clear what the actual problem is.
It sounds to me that you are using Core Data (PersonStore
) to persist your incoming data.
If you are making Core Data calls from the same thread that connectionDidFinishLoading:
is being called on, you are most likely running into a threading issue where Core Data is not happy that you call it from a thread other than the main thread.
Give this a try: in your connectionDidFinishLoading:
wrap your code in the following:
dispatch_async(dispatch_get_main_queue(), ^{
// Include the code here that walks over the incoming JSON
// and creates new `Person` instances.
});
This will execute everything in that block, on the main thread. Which is generally a good idea for Core Data usage. (Probably even required, check the documentation, there is a special section on Core Data & Threads if I remember correctly).
Very curious if this does the trick.
Old answer:
The output of vardump
is not actually valid JSON. You need to use the json_encode()
function instead.
You can turn the NSData
that you received from the server into a string by doing something like this:
if let s = String(data: data, encoding: NSUTF8StringEncoding) {
println(s)
}
You did not mention if you work in Swift or Objective-C but the above translates easily to Objective-C.
NSJsonSerialzation not parsing results from Facebook - Cocoa error 3840
Actually reading documentation could have helped here.
@param result The result of the request. This is a translation of
JSON data to `NSDictionary` and `NSArray` objects. This
is nil if there was an error.
It parses everything itself, I don't even need to do that. So it returns valid NSDictionary or NSArray.
AFNetworking error :The operation couldn’t be completed. (Cocoa error 3840.)
Cocoa error 3840 is a JSON parsing error (if you search on Stack Overflow for it you'll come across this existing question about).
I suggest you run your output JSON from your web service through a JSON validator to check that it is actually compliant with the spec.
Related Topics
Resize Textfield Based on Content
Cocoapods: Failed to Connect to Github to Update the Cocoapods/Specs Specs Repo
Usage of Protocols as Array Types and Function Parameters in Swift
Send a File as Attachment in Objective C
Why am I Getting Ibtool Failed with Exit Code 255
How to Load Resource in Cocoapods Resource_Bundle
Can't Pass Data Correctly to Modal Presentation Using Foreach and Coredata in Swiftui
Swift Get String Between 2 Strings in a String
Nsdateformatter Milliseconds Bug
Pod Install Is Staying on "Setting Up Cocoapods Master Repo"
iPhone Ad Hoc Build Using Xcode 4
Change String Color with Nsattributedstring
Missing Cfbundleiconname in Xcode9 iOS11 App Release
Xcode 4/iOS - Send an Email Using Smtp from Inside My App
How to Maintain Presenting View Controller's Orientation When Dismissing Modal View Controller
Keyboard and Cursor Show, But I Can't Type Inside Uitextfields and Uitextviews