How to get the value from the NsDictionary - ios

I am trying to get the value from the dictionary but its giving me null. Actually I have parsed the XML and then convert it to the dictionary Following is the dictionary after conversion :(I got it in log)
dictionary: {
"soap:Envelope" = {
"soap:Body" = {
GetServerStatusResponse = {
GetServerStatusResult = {
text = "<NewDataSet>\n <Table1>\n <ServerStatus>Standby</ServerStatus>\n </Table1>\n</NewDataSet>";
};
xmlns = "http://tempuri.org/";
};
};
"xmlns:soap" = "http://schemas.xmlsoap.org/soap/envelope/";
"xmlns:xsd" = "http://www.w3.org/2001/XMLSchema";
"xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance";
};
}
now I want to take the value from the ServerStatus tag. That is in this case , "StandBy".
But I am getting the null value in response. Please help? Is there a problem of spaces?

There is no key "ServerStatus" in your dictionary.
The only key is "soap:Envelope"
and "soap:Body", "xmlns:soap", "xmlns:xsd" and "xmlns:xsi" are keys of the dictionary corresponding to the key "soap:Envelope".
You can't access easily to "ServerStatus", what you can do is acceding to text and then parse the value associated to retrieve "ServerStatus"
You should be able to get the value in text this way :
[[[[[dictionary objectForKey:#"soap:Envelope"] objectForKey:#"soap:Body"] objectForKey:#"GetServerStatusResponse"] objectForKey:#"GetServerStatusResult"] objectForKey:#"text"];

You can get to the text in GetServerStatusResult using the following code:
NSDictionary *soapEnvelope = [dictionary valueForKey:#"soap:Envelope"];
NSDictionary *soapBody = [soapEnvelope valueForKey:#"soap:Body"];
NSDictionary *statusResponse = [soapBody valueForKey:#"GetServerStatusResponse"];
NSDictionary *statusResult = [statusResponse valueForKey:#"GetServerStatusResult"];
NSString *xmlString = [statusResult valueForKey:#"text"];
NSDictionary *textDictionary = [XMLDictionary dictionaryWithXMLString:xmlString];
Now the xml in text key is converted to NSDictionary and you can easily get the value of ServerStatus

Related

How to create dynamic NSDictionary to store multiple URLs with multiple keys?

I want to store multiple urls with it's name , websitename and rssfeed url .
How can I store it in dictionary ?
Like ,
My key is , #"nameoffeed", #"websitename", #"urlfeedname" I want to store all in dictionary related to url .
Like user search 3 feed then name , websitename and urlfeed all are stored in NSDictionary ?
How can I do this ?
I am using this , but it stores only 1 value .
//newdevice1 is my nsmanageobject
NSArray *keys = [[[newDevice1 entity] attributesByName] allKeys];
NSDictionary *singledict = [newDevice1 dictionaryWithValuesForKeys:keys];
I need this type of result in dictionary.
{
nameoffeed = "cnn" ;
websitename = "www.cnn.com";
urlfeedname = "www.cnn.com/feed";
nameoffeed = "nytimes" ;
websitename = "www.nytimes.com";
urlfeedname = "www.nytimes.com/feed";
}
you can do by this way.
NSDictionary *dic = #{#"nameoffeed":#"cnn",#"websitename":#"www.cnn.com",#"urlfeedname":#"www.cnn.com/feed"};
NSDictionary *dic2 = #{#"nameoffeed":#"nytimes",#"websitename":#"www.nytimes.com",#"urlfeedname":#"www.nytimes.com/feed"};
NSArray *dicArray = #[dic,dic2];
My key is , #"nameoffeed", #"websitename", #"urlfeedname" I want to
store all in dictionary related to url . Like user search 3 feed then
name , websitename and urlfeed all are stored in NSDictionary ?
Based on this, I believe you want a dictionary of dictionaries, where each sub-dictionary is indexed by the name.
In your example, you are already creating the dictionary for an individual object using dictionaryWithValuesForKeys.
All you need to do is add each one to a dictionary by the url key.
First, you need a mutable dictionary to add items to...
NSMutableDictionary *byURL = [[NSMutableDictionary alloc] init];
Then, for each item, grab the "url" you will use as a key...
NSString *name = [newDevice1 valueForKey:#"nameoffeed"];
and add the values to the dictionary
if (name) {
NSArray *keys = [[[newDevice1 entity] attributesByName] allKeys];
byURL[name] = [newDevice1 dictionaryWithValuesForKeys:keys];
}
You can then do the same thing for each object, resulting in a dictionary where you can look up each object by url.
NSDictionary *d = byURL[#"cnn"];
will give you the dictionary containing all the key/value pairs for "cnn"
If you want to end up with an immutable dictionary that is to be used strictly for searching (after adding everything you want to add), you can create an immutable copy...
NSDictionary *objectsByURL = [byURL copy];
In Dictionary we can save or store the multiple key values.But it must be the Different key.The Key name must not be the same.If use store the same key name in dictionary,it shows you error or your app crashes.But you can have the same values in dictionary.You have to store the different key name with multiple same values.
You can save the save the set of dictionaries to array like below
NSDictionary *dictFirstSet = [[NSDictionary alloc]initWithObjectsAndKeys:#"cnn",#"nameoffeed",#"www.cnn.com",#"websitename",#"www.cnn.com/feed",#"urlfeedname",nil];
NSDictionary *dictSecondSet = [[NSDictionary alloc]initWithObjectsAndKeys:#"nytimes",#"nameoffeed",#"www.nytimes.com",#"websitename",#"www.nytimes.com/feed",#"urlfeedname",nil];
NSArray *arrayDict = [[NSArray alloc]initWithObjects:dictFirstSet,dictSecondSet,nil];
NSLog(#"The arrayDict is - %#",arrayDict);
The output is
The arrayDict is - (
{
nameoffeed = cnn;
urlfeedname = "www.cnn.com/feed";
websitename = "www.cnn.com";
},
{
nameoffeed = nytimes;
urlfeedname = "www.nytimes.com/feed";
websitename = "www.nytimes.com";
}
)

Parsing Json Output correctly

I am trying to correctly target the elements within the Json Output and I am getting closer but I presume there is a easy and obvious way I am missing.
My Json looks like this with a upper level event.
JSON SNIPPET UPDATED
chat = (
(
{
Key = senderId;
Value = {
Type = 0;
Value = "eu-west-1:91afbc3f-890a-4160-8903-688bf0e9efe8";
};
},
{
Key = chatId;
Value = {
Type = 0;
Value = "eu-west-1:be6457ce-bac1-412d-9307-e375e52e22ff";
};
},
{
Key = timestamp;
Value = {
Type = 1;
Value = 1430431197;
};
},
//Continued
I am targeting this level using
NSArray *chat = array[#"chat"];
for ( NSDictionary *theCourse in chat )
{
NSLog(#"---- %#", theCourse);
// I tried the following to target the values
//NSLog(#"chatId: %#", [theCourse valueForKey:#"Key"]);
//NSLog(#"timestamp: %#", theCourse[#"senderId"] );
}
}
I need to parse the value data for each key which if I was using an array would do like [theCourse valueForKey:#"Key"] but I think I may not be going deep enough?
As you would expect, [theCourse valueForKey:#"Key"] gives me the Key values but I need the associate values of those keys.
You can create an easier dictionary:
NSArray *chat = array[#"chat"][0];
NSMutableDictionary* newDict = [NSMutableDictionary dictionary];
for (NSDictionary* d in chat)
[newDict setValue:d[#"Value"][#"Value"] forKey:d[#"Key"]];
Now you can use the newDict.
NSLog(#"chatId: %#", [newDict valueForKey:#"chatId"]);

Cannot access key in dictionary

I have an NSDictionary and am trying to access the 'venue' key and assign various keys such as name. I've tried to take the dictionary and access venue with no luck:
NSDictionary *dic = result;
NSArray *venues1 = [dic valueForKeyPath:#"response.groups.items.venue"];
How would I access venue?
{
meta = {
code = 200;
};
response = {
groups = (
{
items = (
{
venue = {
name = "Ping Tom Memorial Park";
you can access it in this way
NSDictionary *venues1 = dic[#"response"][#"groups"][0][#"items"][0][#"venue"];
be careful that groups, items are arrays and venue is a dictionary instead of array.

JSON Parsing Issue

Hello I am new to iOS development. I am trying to parse a JSON response. Below is the top part of the response:
Table =
{
Rows = {
results = (
{
Cells = {
results = (
{
Key = Rank;
Value = "6.251145362854";
ValueType = "Edm.Double";
"__metadata" = {
type = "SP.KeyValue";
};
},
{
Key = DocId;
Value = 978473;
ValueType = "Edm.Int64";
"__metadata" =
{
type = "SP.KeyValue";
};
},
{
Key = WorkId;
Value = 978473;
ValueType = "Edm.Int64";
"__metadata" = {
type = "SP.KeyValue";
};
},
{
Key = Title;
Value = "Vneea Ready!";
ValueType = "Edm.String";
"__metadata" =
{
type = "SP.KeyValue";
};
},.........................
Now I am using
NSError *error;
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
NSDictionary *results = jsonObject[#"Table"][#"Rows"][#"results"];
So I was able to refine it until here, but then I use
NSDictionary *results = jsonObject[#"Table"][#"Rows"][#"results"][#"Cells"];
When I am going further for Cells and results it is giving me Empty element Error,
After referring to this post JSON parsing using NSJSONSerialization in iOS, it seems like "(" means an array in the response, but it is not working for me. Can someone help me, please?
results is an array, not a dictionary, so you cannot access its contents by name. Your JSON doesn't look well formed, though, because Key should be a string ("Title" not Title).
Each element in the results array is a dictionary, so to get the Value that corresponds to Title you can use
NSArray *results=jsonObject[#"Table"][#"Rows"][#"results"];
NSDictionary *result=[results objectAtIndex:0]; // access the first result
for (NSDictionary *result in results) {
if ([result[#"Key"] isEqualToString:#"Title"]) {
NSLog(#"The value of Title is %#",result[#"Value"]);
break;
}
}

JSon parsing error IOS with empty object key

Hi I have this JSON I'm trying to parse using NSDictionary and trying to get the values as mentioned . but the problem is i do not get a key when i further go into the JSON is there any way to remove this key to go more deep.
NSDictionary *Allinfo = [json objectForKey:#"Collection"];//I go into "Collection"
NSLog(#"All with response array: %#", Allinfo);
NSDictionary *datainfo = [Allinfo objectForKey:#"contact"];//i go into "contact"
after which i get
Data : (
{
attributeList = {
attribute = (
{
name = "display-name";
value = "tel:+";
},
{
name = capabilities;
value = "TRANSFER";
},
{
name = relationship;
value = ACCEPTED;
}
);
resourceURL = "https://example.com";
};
contactId = "tel:+";
},
{
attributeList = {
attribute = (
{
name = "display-name";
value = bob;
},
{
name = capabilities;
value = "TRANSFER";
},
{
name = relationship;
value = ACCEPTED;
}
);
resourceURL = "https://example.com";
};
contactId = "tel:+";
}
)
This starts from a { and i do not get a key for the next object so that i can get the values inside the JSOn
Is there any easier way to remove this or any other so that i can get values like name and value and store them in an array.
Thanks in advance
You have keys for all.
In fact you seem to get confused on array which is a part of attribute.
The outermost key is Data which contains array.
For each object of the array you have :
The first key is attributeList & contactD.
The value of attribute key is an array of 3 values. Each array contains key value pairs. keys are name & value.
I am not sure from where you got [Allinfo objectForKey:#"contact"]. Try to parse like below..
NSDictionary *Allinfo = [json objectForKey:#"Collection"];
NSArray *dataArray = [Allinfo objectForKey:#"Data"];
for(NSDictionary *dic in dataArray)
{
NSDictionary *attributeList=[dic objectForKey:#"attributeList"];
NSArray *attributeArray=[attributeList objectForKey:#"attribute"];
NSString *resourceURLString=[attributeList objectForKey:#"resourceURL"];
NSString *contactIdString=[dic objectForKey:#"contactId"];
for(NSDictionary *attributeDic in attributeArray)
{
NSString *nameString=[attributeDic objectForKey:#"name"];
NSString *valueString=[attributeDic objectForKey:#"value"];
}
}
You can use the following method for this
NSError *e = nil;
NSArray *itemArray = [NSJSONSerialization JSONObjectWithData:[response dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &e];
if (!itemArray) {
NSLog(#"Error parsing JSON: %#", e);
} else {
for(NSDictionary *item in itemArray) {
//Your Data
}
}

Resources