I've combed the questions on here hoping to find a similar situation that I am in, but couldn't find one. My question deals with the NSJSONSerialization class in iOS 5, and how to handle parsing a single value returned from the JSONObjectWithData:options:error: method. The JSON data returned is a single value in an array that looks like this:
[1]
The data will either be 1 (as seen above) or 0, depending on the logic being done in the web service I'm using. As a side note, the web service is a WCF REST service. In XCode, the debugger displays the following after the deserialized JSON is assigned to a temporary NSArray object:
po parsedData
(NSArray *) $43 = 0x06e2ee70 <__NSCFArray 0x6e2ee70>( 1 )
When I try to get the value from the array using ObjectAtIndex:, I get this:
po [parsedData objectAtIndex:0]
(id) $44 = 0x06b1dad0 1
My question is: what is the hex value (0x06b1dad0) before the 1? Maybe a key or index that I am not accessing (I was thinking that I just didn't go far enough into the array to get the real value)? Does it have something to do with how the JSON is formatted? As I've been thinking about it, I have a feeling that the formatting is off.
The problem I am having is that I cannot get the actual value that is stored in the JSON message--when I access the element in the array parsedData and assign it to a variable, it is returning just the hex value and not 1.
Casting to NSInteger gives this:
po (NSInteger)[parsedData objectAtIndex:0]
(NSInteger) $45 = 112319184 1
Any insight into this would be greatly appreciated. I apologize if this has been addressed elsewhere in the forum.
Thanks.
Its likely that the object is an NSNumber try this:
[[parsedData objectAtIndex:0] intValue];
Related
In my app Web services are created in dot net and i am consuming those and I am getting response.In that all the fields like company,type,location everything are strings and there is no problem with this..And there is one more field called Exhibit number actually it is a Integer but they are created as string only.While I am displaying this it is showing zero instead of that number.. Here is my code...
//Storing into Array
[SurveyFilesArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[dic objectForKey:#"FileName"],#"FileName",[dic objectForKey:#"ExibhitNumber"],#"ExhibitNumber",[dic objectForKey:#"Description"],#"Description",[dic objectForKey:#"FileQuality"],#"FileQuality",nil]];
//Retrieving from Array..
NSLog(#"???%#???",[NSString stringWithFormat:#"%d",[[[SurveyFilesArray objectAtIndex:indexPath.row]objectForKey:#"ExibhitNumber"]intValue]]);
NSLog(#"%d",[[[SurveyFilesArray objectAtIndex:indexPath.row]objectForKey:#"ExibhitNumber"]intValue]);
You have typos in your code.
In the order of your code:
Ex-ib-hit-Number
Ex-hib-it-Number
These are 2 different strings.
In your example code you save it correctly written as Ex-hib-it. But you try to access it with ex-ib-hit afterwards. This cannot work.
Say I have a property list that I store into an NSDictionary. An example output from NSLog follows:
2010-12-05 15:26:26.631 TestApp[598:207] Test contents: {
Address = "";
Name = "Test Dictionary";
What exactly would the value for Address be considered? I have heard many possibilities, but I'm not sure exactly. Is it NSNull, nil, what? What I would ultimately like to do is to create a second NSDictionary that filters out all of these blank values, as seen here. But first, I need to figure out what the blank value is counted as, so then I can parse my plist for them, and then discount the keys associated with them.
An empty string is a string, not NSNull or nil. So one way to test would be [#"" isEqualToString: thevalue].
I'm trying to consume an ASP.net Web service, and found a utility called WSDL2ObjC.
Now I'm trying to use it, and basic use is working (asking for simple data types, such as booleans or strings), but now I'm requesting an array of the "EmailServiceSvc_Email" structure, which contains "subject", "from" and "message" properties.
The app won't compile, with the message above, and here's the code on wich it gets stuck:
EmailServiceSvc_Email * eml = (EmailServiceSvc_Email *) arrEmails[[indexPath row]];
This is the explanation of the variables:
eml : the new variable I'm trying to get out of arrEmails.
arrEmails: an NSMutableArray of the EmailServiceSvc_Email object
indexPath: the indexPath parameter, this is from the cellForRowAtIndexPath function.
As you can see probably, I want to display all the "subject"s from the "EmailServiceSvc_Email" structure in a TableView, on the iPhone.
To get an element out of an NSMutableArray you must use -objectAtIndex:. The a[i] syntax does not work with NSArrays.
EmailServiceSvc_Email* eml = [arrEmails objectAtIndex:indexPath.row];
I wrote a tutorial about Wsdl2objc and complex types:
http://brismith66.blogspot.com/2010/07/iphone-development-accessing-soap.html
EDIT*
hi guys , lets say i have an array with 5 Strings = "1","2","3","4","5".
Im also doing parsing of xml. So how do i check whether a parse xml value is the same value of either one of the objects IN the array?
solved * i put them in a for loop to, looping through each array.value and making a root (if else) that checks whether the value in the array is equal to the parsed xml value
I'm not sure about parsing an xml with attributes. have seen a similar question here
But it shows to get an attribute of intValue. But i need to get the attributes of string type,How to do that?? Images of xml and the relevant portions are given in the following links
Click here for xml and here for required data
This answer to the linked question should work for you as well. The contents of attributeDict are already NStrings. All that is going on extra in the linked answer that they are calling the intValue method on the returned NSString to parse that string into an int. In your case, you don't need this little bit of an extra step. If you just do this:
NSString * stringValue = [attributeDict objectForKey:#"attribute"];
you'll have the value of the attribute called "attribute" in a string.