I am using the flight aware service to get flight information but I am having trouble getting the data in a way I can use.
Json response is:
{
"AirlineInfoResult": {
"name": "US Airways",
"shortname": "US Airways",
"callsign": "Cactus",
"location": "Tempe, AZ",
"country": "United States",
"url": "http://www.usairways.com/",
"phone": "+1-800-428-4322"
}
}
And this is the code I am using but nothing is returned
NSMutableDictionary * routeRes;
routeRes = [json objectForKey: #"AirlineInfoResult"];
NSMutableArray * res;
res = [json objectForKey: #"url"];
for (NSMutableDictionary * flight in res) {
NSLog(#"actual arrival time= %#", [flight objectForKey: #"url"]);
}
What am I missing in getting this data?
change below line:
res = [json objectForKey:#"url"];
with
res = [routeRes objectForKey:#"url"];
Related
This is my first time asking a question here, so please excuse my lack of writing etiquette.
I have a jSON data file which looks like this:
[ {
"category": "Feline",
"text": "CAT",
"definition": "Meow" },
{
"category": "Mammal",
"text": "DOG",
"definition": "Bark" },
{
"category": "Insect",
"text": "ANT ",
"definition": "buzz" },
{
"category": "Insect'",
"text": "MOSQUITO ",
"definition": "buzz" },
{
"category": "Number'",
"text": "10",
"definition": "ten" }
]
I am importing it as follows, the dataList is a NSMutableArray, I am also confused if using the MutableArray is good or if I should be using NSDictionary?:
-(void)readDataFromFile {
NSString * filePath =[[NSBundle mainBundle] pathForResource:#"data" ofType:#"json"];
NSError * error;
NSString* fileContents =[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if(error)
{
NSLog(#"Error reading file: %#",error.localizedDescription);
}
self.dataList = (NSMutableArray *)[NSJSONSerialization
JSONObjectWithData:[fileContents dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:NULL];
}
I would like to sort the JSON data alphabetically according to 'text'
and also I am using sections and index list. I mainly followed https://www.appcoda.com/ios-programming-index-list-uitableview/ tutorial.
I am confused on how to sort the JSON data and also divide them into sections of alphabets and also iOS contacts accounts for numbers by putting them under '#' section.
How do i accomplish that as well?
Thanks in advance!
In the below code i've made an 2D array of 27 elements(1 for numeric
and 26 for alphabets) and then placed the value according to the
string initial character. This piece of code worked for me.Hope so it
will work for you too :)
for (int section = 0; section < 27; section++)
{
[arrSections addObject:[NSMutableArray array]];
}
for (NSDictionary *item in SampleDictionary)
{
NSString *KeyValue = [item[#"DictionaryKEy"] capitalizedString];
*//In your case its Text*
int ascii = (int)[KeyValue characterAtIndex:kZero];
if (ascii >= 65 && ascii <= 90)
{
[arrSections[(ascii - 64)] addObject:item];
}
else
{
[arrSections[0] addObject:item];
}
}
This is my json web response response.
-Itinerary: [
-{
ID: "1",
ValueOne: "2473.05",
ValueTwo: "368.95"
},
-{
ID: "2",
ValueOne: "2453.05",
ValueTwo: "308.95"
}
],
-Details :[
-{
ID:"1",
FirstName:"Graham",
LastName:"Json"
},
-{
ID:"1",
FirstName:"Anuradh",
LastName:"Stackoverflow"
}
]
What should be the response object type.I do like this, is it correct or it does not matter whatever the type
NSDictionary *Results = (NSDictionary *)responseObject;
then I get the response.now what I want is to get itinerary details and
get the Details only where the itinerary ID is equal to Details ID .how can I do that.
NSArray *Itinerary = [Resulsts objectForKey=#"Itinerary"];
NSArray *Itinerary = [Resulsts objectForKey=#"Detailss"];
for(NSDictionary *itinry in Itinerary)
{
NSString *Iid = [itinry objectForKey="ID"];
NSString *ValOne = [itinry objectForKey="ValueOne"];
//like this I'm getting values
//then here I want to get all details which it's `itinerary` `ID` equal to `Details` `ID`.how can I do that.
}
then here I want to get all details which it's itinerary ID equal to Details ID.how can I do that.
Please try this code Hope it will work for you
NSArray *Itinerary = [Resulsts objectForKey=#"Itinerary"];
NSArray *dTinerary = [Resulsts objectForKey=#"Detailss"];
for(int i=0; i<Itinerary;i++)
{
NSString *iID=[[Itinerary objectAtIndex:i] objectForKey:#"ID"];
NSString *dID=[[dTinerary objectAtIndex:i] objectForKey:#"ID"];
if ([iID isEqualToString:dID])
{
// do your thinks here
NSLog(#"%#",[[Itinerary objectAtIndex:i] objectForKey:#"ValueOne"]);
// break;
}
// NSString *Iid = [itinry objectForKey="ID"];
// NSString *ValOne = [itinry objectForKey="ValueOne"];
//like this I'm getting values
//then here I want to get all details which it's `itinerary` `ID` equal to `Details` `ID`.how can I do that.
}
I am stuck with JSON parsing in my code. I tried a lot but could not figure out what is going wrong.
This is my JSON response
[{
"order": {
"transaction_id": "bFnRjTPYfD",
"status": 0,
"created_at": "2015-04-22 09:35:35"
}
}, {
"order_items": [{
"item_id": 1,
"item_name": "Potato",
"item_description": null,
"item_salesprice": 14,
"item_imagepath": "\/uploads\/veggies\/potato.jpg",
"order_quantity": "4 Kg",
"order_price": 69.99
}, {
"item_id": 2,
"item_name": "Tomato",
"item_description": null,
"item_salesprice": 18,
"item_imagepath": "\/uploads\/veggies\/tomato.jpg",
"order_quantity": "6 Kg",
"order_price": 79.99
}]
}]
I want to extract transaction id and item name. Can you please help me with this?
Update Answer:
NSDictionary *Order=[JSONData ValueForKey:#"Order"];
NSString *transaction_id=[Order ValueForKey:#"Transaction_id"];
You can get transaction_id by this way,
NSDictionary *OrderJson=[jsonArray objectAtIndex:0];
NSDictionary *OrderDic = [OrderJson valueForKey:#"order"];
NSString *transactionId=[OrderDic objectForKey:#"transaction_id"];
ItemNames are in array, you can get it by for loop, or predicates, Here I show you with for
NSDictionary *order_itemsJson=[jsonArray objectAtIndex:1];
NSArray *item=[order_itemsJson objectForKey:#"order_items"];
for (NSDictionary *dic in item) {
NSLog(#"%#",[dic valueForKey:#"item_name"]);
}
Enjoy Coding !!
Do the following things, you will get output.
NSString* transectionId = [[[arrayResponse objectAtIndex:0] valueForKey:#"order"] valueForKey:#"transaction_id"];
NSLog(#"transectionId : %#",transectionId);
NSArray* arrayInner = [[arrayResponse objectAtIndex:1] valueForKey:#"order_items"];
for (int i=0; i<arrayInner.count; i++) {
NSDictionary* dictInner = [arrayInner objectAtIndex:i];
NSString* itemName = [dictInner valueForKey:#"item_name"];
NSLog(#"Item Name : %#",itemName);
}
Here you will get transaction_id and item_name
Hope this help you.
Try it.. For Transaction id..
NSDictionary *Order=[JSONData ObjectForKey:#"Order"];
NSString *transaction_id=[Order ValueForKey:#"Transaction_id"];
and For item_id try,...
NSDictionary *item=[JSONData objectForKey:#"order_items"];
NSArray *item_id=[Order ValueForKey:#"item_name"];
when i try parse the local json file it returns null value am using xcode6.2 beta and ios 8.1
I have followed this posts also How to read a JSON file using the NSJSONSerialization Class Reference?Cocoa-Touch - How parse local Json file2
My code is
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:#"countries"
ofType:#"json"];
NSError *error = nil;
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:NULL];
id json = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];
NSLog(#"JSON: %#", json);
//Upto myJson string value the json file converted into string but the jsonserialization only have //the problem and the response is always null.
//please give some suggestions,
//Thanks in advance,
my json file contains this values
{
"AD" = "Andorra";
"AE" = "United Arab Emirates";
"AF" = "Afghanistan";
"AG" = "Antigua and Barbuda";
"AI" = "Anguilla";
"AL" = "Albania";
"AM" = "Armenia";
}
Your json file is corrupted. Replace your json file with this and it will work
{
"AD": "Andorra",
"AE": "United Arab Emirates",
"AF": "Afghanistan",
"AG": "Antigua and Barbuda",
"AI": "Anguilla",
"AL": "Albania",
"AM": "Armenia"
}
To validate your JSON file you can go for this link
Your Json is not valid. Check your Json here
You have put following Json in your JsonFile.
{
"AD" : "Andorra",
"AE" : "United Arab Emirates",
"AF" : "Afghanistan",
"AG" : "Antigua and Barbuda",
"AI" : "Anguilla",
"AL" : "Albania",
"AM" : "Armenia"
}
Here I am using Sqlite Database for my Application, and my Database Table contains the column keyjson and values in rows are like below
{"filename":"21-01-3-02","label":""}
{"filename":"02-C-4-1","label":"17"}
{"filename":"14-L-3-2","label":"Nord Est"}
{"filename":"02-Z-5-1NouvelleIndustrieSectionValton","label":"1810"}
json data sas string
now I need to particular string that like
02
1
2
1NouvelleIndustrieSectionValton
from that particular column .
can anyone know the query for it,
Please replay me
®ards
You can do that without SQLite query. First, convert your JSON to a NSMutableArray :
NSData *data = [yourJsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableArray *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
Then you can parse it easily as follow :
NSMutableArray *finalResult = [NSMutableArray array];
NSString *filename;
for (id e in res){
filename = [e objectForKey:#"filename"];
[finalResult addObject:[[filename componentsSeparatedByString:#"-"] lastObject]];
}
// Here we go, finalResult contains the values you want
The problem is that your "JSON" is not valid JSON. It should look like this if you want it works :
[
{
"filename": "21-01-3-02",
"label": ""
},
{
"filename": "02-C-4-1",
"label": "17"
},
{
"filename": "14-L-3-2",
"label": "Nord Est"
},
{
"filename": "02-Z-5-1NouvelleIndustrieSectionValton",
"label": "1810"
}
]