JSON Parse error Objective-c [closed] - objective-c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
i trying to parse JSON with NSJSONSerialization.
NSArray *a = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err]; the error shows this: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Badly formed array around character 202.) UserInfo=0x9f79230 {NSDebugDescription=Badly formed array around character 202.}
here is the JSON code:
[
{
"ID":1,
"name":"sometext",
"c":"sometext",
"city":"sometext",
"street":"sometext",
"a":32.914671,
"b":35.292417,
"Info":"sometext",
"imageAddress":"images/aroma.jpeg"
}
{
"ID":2,
"name":"sometext",
"c":"sometext",
"city":"sometext",
"street":"sometext",
"a":4.224,
"b":72.1234,
"Info":"sometext",
"imageAddress":"images/"
}
where the JSON not coded currently?

In your JSON, array objects are not separated by comma
[
{
"ID":1,
"name":"sometext",
"c":"sometext",
"city":"sometext",
"street":"sometext",
"a":32.914671,
"b":35.292417,
"Info":"sometext",
"imageAddress":"images/aroma.jpeg"
},
{
"ID":2,
"name":"sometext",
"c":"sometext",
"city":"sometext",
"street":"sometext",
"a":4.224,
"b":72.1234,
"Info":"sometext",
"imageAddress":"images/"
}
]
This should work.

Related

Karate Api : check if a phrase is available response object array

I've a response
{ errors: [
{
code: 123,
reason: "this is the cause for a random problem where the last part of this string is dynamically generated"
} ,
{
code: 234,
reason: "Some other error for another random reason"
}
...
...
}
Now when I validate this response
I use following
...
...
And match response.errors[*].reason contains "this is the cause"
This validation fails, because there is an equality check for complete String for every reason ,
I all I want is, to validate that inside the errors array, if there is any error object, which has a reason string type property, starting with this is the cause phrase.
I tried few wild cards but didn't work either, how to do it ?
For complex things like this, just switch to JS.
* def found = response.errors.find(x => x.reason.startsWith('this is the cause'))
* match found == { code: 123, reason: '#string' }
# you can also do
* if (found) karate.log('found')
Any questions :)

Acumatica. Error during updating the Customer's Credit Limit

Could you help me with the following question?
I try to update the Customer Credit Limit in Acumatica:
Request:
PUT https://{Base_URL}/Customer?$select=CreditVerificationRules&$filter=CustomerID%20eq%20'0e31e8a5-08e3-ea11-a828-000d3ab2c384'
{
"CreditVerificationRules": {
"CreditLimit": {
"value": 1234.0
}
}
}
But I receive the following error:
Response: {"message":"An error has occurred.","exceptionMessage":"Syntax error: character '%' is not valid at position 10 in 'CustomerID%20eq%20'0e31e8a5-08e3-ea11-a828-000d3ab2c384'
Try it without the html encoding. I.e. CustomerID eq '0e31e8a5-08e3-ea11-a828-000d3ab2c384' FYI, if this was an instance where you needed html encoding, then you should have encoded the ' symbol as well.

Extra JSON value to display in Objective C

In my app, I get the following response for a failed user login:
{
"non_field_errors": [
"User account is disabled."
]
}
Is there a way to get this in Objective-C to only display: User account is disabled.?
When I do the following, it displays:
(
"User account is disabled."
)
Code:
NSLog(#"%#",[JSONValue objectForKey:#"non_field_errors"]);
How can I remove the parentheses and double quotes?
Parentheses is there because you are getting response in such a way. you can access your data using following way.
JSONValue[#"non_field_errors"][0] // This will return User account is disabled.
That is because the value for key non_field_errors is a JSON Array (Notice the square brackets). So use this :
NSArray *arr = [JSONValue objectForKey:#"non_field_errors"];
NSLog(#"%#",[arr firstObject]);
or just cram it all in one line
[(NSArray*)[JSONValue objectForKey:#"non_field_errors"] firstObject];

Google Directions Json Error

I'm trying to get the directions using google directions but I'm getting this error :
Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised
leading character (41)\
Here is the Json
loadDirections('Avenida Paulista', 'Rua Augusta', { 'locale': 'en', travelMode: G_TRAVEL_MODE_WALKING, avoidHighways: false, getPolyline: true, getSteps: true, preserveViewport: false})
Whats is wrong? Whats Unrecognised leading character (41) mean?
- (void)loadWithStartPoint:(NSString *)startPoint endPoint:(NSMutableArray *)endPoints options:(UICGDirectionsOptions *)options {
for (int idx = 0; idx < [endPoints count];idx ++)
{
NSString* msg = [NSString stringWithFormat:#"loadDirections('%#', '%#', %#)", startPoint, [endPoints objectAtIndex:idx], [options JSONRepresentation]];
NSLog(#"msg %#",msg);
mstr = [msg retain];
[self performSelector:#selector(loadDirections:) withObject:nil afterDelay:0.5];
}
}
-(void)loadDirections:(NSString *)message
{
[googleMapsAPI stringByEvaluatingJavaScriptFromString:mstr];
}
The JSON is invalid.
{
'locale': 'en',
travelMode: "G_TRAVEL_MODE_WALKING",
avoidHighways: false,
getPolyline: true,
getSteps: true,
preserveViewport: false
}
I think this has something to do with G_TRAVEL_MODE_WALKING, it should be in quotes. The only valid JSON values are as follows
Objects ({})
Arrays ([])
Value
String ("this is a string")
Number 1/-1
true
false
null
JSON Documentation
Make sure that, while initializing the GDirections object, you also pass an actual element as the second parameter.
As stated in the documentation:
The mode of travel, such as driving (default) or walking. Note that if
you specify walking directions, you will need to specify a panel
to hold a warning notice to users.
If you are using the Map-Kit-Route-Directions extensions, you just have to open the api.html file and, at line 16, substitute this
gdir = new GDirections(null, null);
with this
gdir = new GDirections(null, "body");

Parsing JSON file using JSONKit

I am constructing a tuning fork app. The fork should allow up to 12 preset pitches.
Moreover, I wish to allow the user to choose a theme. Each theme will load a set of presets (not necessary to use all of them).
My configuration file would look something like this*:
theme: "A3"
comment: "An octave below concert pitch (ie A4 440Hz)"
presets: {
A3 220Hz=220.0
}
// http://en.wikipedia.org/wiki/Guitar_tuning
theme: "Guitar Standard Tuning"
comment:"EADGBE using 12-TET tuning"
presets: {
E2=82.41
A2=110.00
D3=146.83
G3=196.00
B3=246.94
E4=329.63
}
theme: "Bass Guitar Standard Tuning"
comment: "EADG using 12-TET tuning"
presets: {
E1=41.204
A2=55.000
D3=73.416
G3=97.999
}
...which need to be extracted into some structure like this:
#class Preset
{
NSString* label;
double freq;
}
#class Theme
{
NSString* label;
NSMutableArray* presets;
}
NSMutableArray* themes;
How do I write my file using JSON? ( I would like to create a minimum of typing on the part of the user -- how succinct can I get it? Could someone give me an example for the first theme? )
And how do I parse it into the structures using https://github.com/johnezang/JSONKit?
Here's a valid JSON example based on your thoughts:
[
{
"name": "Guitar Standard Tuning",
"comment": "EADGBE using 12-TET tuning",
"presets": {
"E2": "82.41",
"A2": "110.00",
"D3": "146.83",
"G3": "196.00",
"B3": "246.94",
"E4": "329.63"
}
},
{
"name": "Bass Guitar Standard Tuning",
"comment": "EADG using 12-TET tuning",
"presets": {
"E1": "41.204",
"A1": "55.000",
"D2": "73.416",
"G2": "97.999"
}
}
]
Read a file and parse using JSONKit:
NSData* jsonData = [NSData dataWithContentsOfFile: path];
JSONDecoder* decoder = [[JSONDecoder alloc]
initWithParseOptions:JKParseOptionNone];
NSArray* json = [decoder objectWithData:jsonData];
After that, you'll have to iterate over the json variable using a for loop.
Using the parser in your question and assuming you have Simeon's string in an NSString variable. Here's how to parse it:
#import "JSONKit.h"
id parsedJSON = [myJSONString objectFromJSONString];
That will give you a hierarchy of arrays and dictionaries that you can walk to get your Preset and Theme objects. In the above case, you would get an array with two dictionaries each with a name, comment and presets key. The first two will have NSString values and the third (presets) will have a dictionary as it's value with the note name as keys and the frequencies as values (as NSString objects).