JSON Value for key is starting with# - objective-c

I have the json response like below:
"hj.tradingResponse" = {
"#approved" = true;
"#qualified" = true;
billingAddress = {
address = "146 W. Main Street";
city = Aguilar;
country = US;
postalCode = 81020;
state = Colorado;
};
I stored a result in a dictionary. Byt when I give value for key [result valueForKey:#"\"#approved\""];
I'm getting null value but I thought #approved is not recognising.
Any idea how to solve this?

In JSON, the quotes themselves are not part of the key.
Try
[result valueForKey:#"#approved"]

Related

Get 1 Segment of a Array

I want to get from my Array only on section.
This is my result...
(
{
foreName = FORENAME1;
gender = female;
id = 15;
key = 12345678901;
longName = LASTNAME1;
name = FORELAST1;
},
{
foreName = FORENAME2;
gender = male;
id = 52;
key = 12345678902;
longName = LASTNAME2;
name = FORELAST2;
},
{
foreName = FORENAME3;
gender = male;
id = 77;
key = 12345678903;
longName = LASTNAME3;
name = FORELAST3;
}
)
How can I search for id 77 and get the whole entry array for id 77 like the following example?
{
foreName = FORENAME3;
gender = male;
id = 77;
key = 12345678903;
longName = LASTNAME3;
name = FORELAST3;
}
That result is an array, and you want the element who's id==77...
- (NSDictionary *)elementInArray:(NSArray *)array withId:(NSNumber *)anId {
// there are a lot prettier ways, but this will be the clearest
for (NSDictionary *d in array) {
if ([d[#"id"] isEqual:anId]) return d;
}
return nil;
}
// call it like this
NSArray *array = // the array logged in the OP
NSDictionary result = [self elementInArray:array withId:#77];
NSLog(#"%#", result);

How to get One value from an array (protractor)?

Is it possible to get One value from an array such as:
Array
(
[id] = 100917
[sid] = SM0ddb860df74148f19f57b314bfc80b39
[date_created] = 2016-02-10 12:04:08
[date_updated] = 2016-02-10 12:04:08
[date_sent] =
[to] = +996559138088
[from] = +15674434357
[body] = Confirmation code: 75ba76
[status] = queued
[direction] = outbound-api
[price] =
[price_unit] = USD
)
I need somehow to get a value of "75ba76" (in confirmation code string) using:
[to] = +996559138088
When i try to select it, i see:
http://prntscr.com/a1cngn
Any ideas?..
Thanks.
You can use string and array operations to get the value that you need, as text in html elements are stored as strings by default. filter() function can be used to get the <pre> element that has the number you specify in arrString variable below. Here's how -
var arrString = "[to] = +996559138088";
var reqValue;
element.all(by.css('pre')).filter(function(ele){
return ele.getText().then(function(str){
return str.search(arrString) > -1;
});
}).getText().then(function(val){ //returns array as a string
reqValue = val.split('\n').filter(function(eachEle){return eachEle.search(/body/i)>-1}).toString().split(' ').pop();
//reqValue holds the data that you require
});
Though code looks a little weird, it should do the job. Hope it helps.
With a strong brainstorm i solved the problem using the next code. It's maybe seems to be huge, but it works great!
it("get temp password", function () {
browser.get("http://api.test.yalla.ng/sms.php?db=stena_kg");
var preElement = element.all(by.css('pre'));
var matchPhone = browser.params.phones.matchPhone;
var matchPass;
var elementIndex = 0;
parseNextElement();
function parseNextElement() {
preElement.get(elementIndex).getText().then(function (text) {
parseElementData(text);
if(!matchPass){
elementIndex++;
parseNextElement();
}else{
onMatch();
}
});
}
function parseElementData(data){
var dataArr = data.split("\n");
var phoneTxt = "";
var passTxt = "";
for(var i = 0; i<dataArr.length; i++){
var str = dataArr[i];
var splitStr = str.split(" ");
if(str.match(/\[to\]/)){
phoneTxt = splitStr[splitStr.length - 1];
}
if(str.match(/\[body\]/)){
passTxt = splitStr[splitStr.length - 1];
}
}
console.log(phoneTxt, passTxt);
if(phoneTxt == matchPhone){
matchPass = passTxt;
}
}
function onMatch(){
console.log("COMPLETE!!!", matchPass);
}
}, 600000);
});

I'm having trouble accessing the value I need from an NSDictionary object

I'm having trouble accessing the value I need in an NSDictionary object, a song has multiple values under the track key, but since dictionaries aren't indexable, I'm unable to access the values individually. I have tried storing the object for the track key as an array but the two entries become a single string entry and therefore the array is only of length 1 and not helpful.
Also, doing something like [response valueForKeyPath#"response.songs.tracks.foreign_id"] gives me both foreign ids, how can I just get the first one? I've been struggling with this for a while, have searched all around stackoverflow for answers but none have worked, any help is appreciated, thanks!
This is the data in the NSDictionary:
{
response = {
songs = (
{
"artist_foreign_ids" = (
{
catalog = spotify;
"foreign_id" = "spotify:artist:6vWDO969PvNqNYHIOW5v0m";
}
);
"artist_id" = AR65K7A1187FB4DAA4;
"artist_name" = "Beyonc\U00e9";
id = SOUTJIT142F256C3B5;
title = "Partition (Explicit Version)";
tracks = (
{
catalog = spotify;
"foreign_id" = "spotify:track:2vPTtiR7x7T6Lr17CE2FAE"; //I WANT TO ACCESS JUST THIS VALUE
"foreign_release_id" = "spotify:album:1hq4Vrcbua3DDBLhuWFEVQ";
id = TRLHJRD1460052F846;
},
{
catalog = spotify;
"foreign_id" = "spotify:track:6m4ZFQb3zPt3IdRDTN3tfb";
"foreign_release_id" = "spotify:album:5KPpho7rztJrZVA9yXjk8K";
id = TRYMBSK146005C3A46;
}
);
}
);
status = {
code = 0;
message = Success;
version = "4.2";
};
};
}
The tracks in your sample dictionary appear to already be an array (of dictionaries), so you should be able to do something like:
NSArray *songs = response[#"songs"];
for (NSDictionary *songDict in songs) {
NSArray *tracks = songDict[#"tracks"];
for (NSDictionary *trackDict in tracks) {
NSString *trackForeignId = trackDict[#"foreign_id"];
// Do whatever you like with trackForeignId here.
}
}

Extracting data from nested NSDictionary's in a json array

I have a json array that I'm trying to extract data from. The array was created using NSJSONSerialization. Here is what the json array looks like from NSLog([jsonArray debugDescription]);:
{
properties = (
{
ID = 12345;
PropertyName = "McDonalds";
key = 00112233445566778899aabbccddeeff;
},
{
ID = 12346;
PropertyName = "Taco Bell";
key = 00112233445566778899aabbccddeef0;
},
{
ID = 12347;
PropertyName = "Burger King";
key = 00112233445566778899aabbccddeef1;
}
);
success = 1;
totalCount = 3;
}
I need to extract each ID and each Property name and dump the values into separate arrays. How can I do this?
You can use Key-Value Coding:
NSArray *ids = [jsonArray valueForKeyPath:#"properties.ID"];
NSArray *propertyNames = [jsonArray valueForKeyPath:#"properties.PropertyName"];

Searching within subsubdictionaries of an array to return the parent dictionary (Objective C)

I have a structured array like the one below
(
{
id = 0;
name = "A";
tables = (
{
comment = "";
id = 0;
name = "T1";
},
{
comment = "";
id = 1;
name = "T2";
},
{
comment = "";
id = 4;
name = "T3";
}
);
},
{
id = 1;
name = "B";
tables = (
{
comment = "";
id = 5;
name = "T1";
},
{
comment = "";
id = 6;
name = "T2";
}
);
}
)
Given that I know the value of id key of one of the dictionaries (let's take id=6), how can I get its parent dictionary or to be more precise the value of name key (in this case B).
I've tried using predicate strings but the problem is that the parent dictionary has also a key named id, so I get the wrong results.
EDIT: id keys are unique (and it is also not necessary that ids of the elements in the first dictionary have lower intiger values than the ones in the dictionaries with higher indexes)
Try this, where array is your structured array
NSDictionary *parentDict=[[NSDictionary alloc]initWithDictionary:nil];
NSString *name=[[NSString alloc]init];
NSArray *table=[[NSArray alloc]initWithArray:nil];
for (NSDictionary * dict in array) {
table=[dict objectForKey:#"tables"];
for (NSDictionary *childDict in table) {
if ([[childDict objectForKey:#"id"]isEqualToString:#"6"]) {
//if your id value is int then use ==6 to check
parentDict=dict;
name=[dict objectForKey:#"name"];
return;
}
}
}