I assign JSON values to NSDictionary and try to retrive Key's from the dictionary. It returns the value with the parentheses!
This is the value it returns
(
873
) , (
"HST 299"
)
Here is the JSON
[{"_id":873,"_code":"HST 299"}]
Here is my code:
NSDictionary *courseDetail = [responseString JSONValue];
NSLog(#"%# , %#", [courseDetail valueForKey:#"_id"], [courseDetail valueForKey:#"_code"]);
Because your JSON is an array ([] means array).
And there is ONE dictionary with TWO key-values in the array.
So, if you change your code into
NSDictionary *courseDetail = [[responseString JSONValue] objectAtIndex:0];
it will gives you the correct result.
Parentheses are how NSArrays describe themselves. Your values are apparently arrays that each contain a single string, not bare strings.
Related
I have NSMutableArray like :
NSMutableArray *x=[[NSMutableArray alloc]initWithObjects:#"a",#"b",#"c",#"d", nil];
NSLog(#"%#",x.description);
From the description I got the following result :
(
a,
b,
c,
d
)
I want to again recreate the array from the description is it possible ?
There is no way that would reliably work in all cases. For example, you cannot know if
(
123
)
represents an array containing a string or a number.
Added: There actually is a method to convert the description back to an array object (but it does not always return an identical array for the reason given above). The description method of NSArray and NSDictionary
uses the Old-Style ASCII Property Lists format which can be read using NSPropertyListSerialization:
NSArray *a1 = #[#"123", #456];
NSString *desc = [a1 description];
NSData *d = [desc dataUsingEncoding:NSUTF8StringEncoding];
NSArray *a2 = [NSPropertyListSerialization propertyListWithData:d
options:0
format:NULL
error:NULL];
NSLog(#"%#", a2);
NSLog(#"a1[1] is a %#", [a1[1] class]);
NSLog(#"a2[1] is a %#", [a2[1] class]);
Output:
(
123,
456
)
a1[1] is a __NSCFNumber
a2[1] is a __NSCFString
As you can see, the array a2 looks like a1, but the second element 456, which was a NSNumber originally, has been read back as a NSString.
So you should use description only as a debugging tool. Better methods to create a reversible human-readable description or an archive have been mentioned in the other answers and comments.
While you could, it's not why NSLog() exists. NSLog's purpose is to give a simple error logging mechanism for developers.
You should read into other means of storing data :
Saving to file via NSData.
Using Core Data
NSCoder
Various JSON serializers/parsers.
etc.
But if you really want to, you could parse a log file manually. (With NSRegularExpressions perhaps, NSScanner, etc.)
Hi i'm returning a json string as follows:
{data,[{"id":1,"text":"blabla"},{"id":2,"text":"blabla2"}]}
When i try to print with NSLog like so:
NSLog(#"id: %#",[temp objectForKey:#"id"]);
i get large numbers, not the id.
If i enclose the the id's in quotes, things are fine.
Is there a way to decode the string when integers don't have quotes?
thx!
Looks like you have to use integerValue method to convert object to integer.
NSLog(#"id: %d",[[temp objectForKey:#"id"]integerValue]);
thxs for everyone's help.
Your formatting is a bit wonky. Also -objectForKey: takes an NSString for an argument, so be sure to prepend the quotes with # to signify an NSString constant.
NSLog(#"id: %#", [temp objectForKey:#"id"]);
The problem is in your string format. Instead of
NSLog(#"id: %#),[temp objectForKey:#"id"];
Try using this:
NSLog(#"id: %d),[temp objectForKey:#"id"];
%# is only for strings, and in your case, when the ID is not quoted, it's an integer and you should use %d instead.
For example, I read a data like this
a\tbcd\tttte\tjjjd\tnjnjnjd\tss\tee
and I want to make a array like this:
{ #"a", #"bcd", #"ttte", #"jjjd", #"njnjnjd", #"ss", #"ee" }
How can I do so? Thank you.
You can use -componentsSeparatedByString:, as in
NSArray *ary = [mystring componentsSeparatedByString:#"\t"];
- (NSArray *)componentsSeparatedByString:(NSString *)separator
componentsSeparatedByString: Returns
an array containing substrings from
the receiver that have been divided by
a given separator.
(NSArray *)componentsSeparatedByString:(NSString
*)separator Parameters separator The separator string. Return Value An
NSArray object containing substrings
from the receiver that have been
divided by separator.
Discussion The substrings in the array
appear in the order they did in the
receiver. Adjacent occurrences of the
separator string produce empty strings
in the result. Similarly, if the
string begins or ends with the
separator, the first or last
substring, respectively, is empty. For
example, this code fragment:
NSString *list = #"Norman, Stanley, Fletcher";
NSArray *listItems = [list componentsSeparatedByString:#", "];
produces an array { #"Norman",#"Stanley", #"Fletcher" }.
If list begins with a comma and
space—for example, ", Norman, Stanley,
Fletcher"—the array has these
contents: { #"", #"Norman",
#"Stanley", #"Fletcher" }
If list has no separators—for example,
"Norman"—the array contains the string
itself, in this case { #"Norman" }.
Availability Available in Mac OS X v10.0 and later.
See Also:
componentsJoinedByString: (NSArray)
– pathComponents
Related Sample Code:
ColorMatching
CoreRecipes
iSpend
iSpendPlugin
QTKitMovieShuffler
Declared In NSString.h
From NSString docs
In Xcode, if I have an NSString containing a number, ie #"12345", how do I split it into an array representing component parts, ie "1", "2", "3", "4", "5"... There is a componentsSeparatedByString on the NSString object, but in this case there is no delimiter...
There is a ready member function of NSString for doing that:
NSString* foo = #"safgafsfhsdhdfs/gfdgdsgsdg/gdfsgsdgsd";
NSArray* stringComponents = [foo componentsSeparatedByString:#"/"];
It may seem like characterAtIndex: would do the trick, but that returns a unichar, which isn't an NSObject-derived data type and so can't be put into an array directly. You'd need to construct a new string with each unichar.
A simpler solution is to use substringWithRange: with 1-character ranges. Run your string through a simple for (int i=0;i<[myString length];i++) loop to add each 1-character range to an NSMutableArray.
A NSString already is an array of it’s components, if by components you mean single characters. Use [string length] to get the length of the string and [string characterAtIndex:] to get the characters.
If you really need an array of string objects with only one character you will have to create that array yourself. Loop over the characters in the string with a for loop, create a new string with a single character using [NSString stringWithFormat:] and add that to your array. But this usually is not necessary.
In your case, since you have no delimiter, you have to get separate chars by
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
or this one
- (unichar)characterAtIndex:(NSUInteger) index inside a loop.
That the only way I see, at the moment.
Don't know if this works for what you want to do but:
const char *foo = [myString UTF8String]
char third_character = foo[2];
Make sure to read the docs on UTF8String
I am having a lengthy string which contains alphabets and a special character like "|". i need to split this strings based on the "|" delimiter and store the individual string in to an array. Is there any string function which helps us to do the same.?
Thanks,
Shibin.
Sounds like you need componentsSeparatedByString:
NSString *string = #"hello|how|are|you";
NSArray *array = [string componentsSeparatedByString:#"|"];
NSLog(#"array: %#", array);
Output:
array: (
hello,
how,
are,
you
)