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"
}
]
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];
}
}
Order of json format is getting changed. I need the below format
{
"user_id": "",
"name": "",
"StDate": "07/16/2015 13:00",
"EdDate": "07/16/2015 13:00",
"detailed": [
{
"Stname": ""
},
]
}
What i am getting atlast is
{
"user_id" : "1",
"Detailed" : [
{
“Stname" : ""
},
"EdDate" : "08\/19\/2015 12:25:47",
"StDate" : "08\/19\/2015 12:25:47",
“name” : "",
}
After getting all values i am converting to json. I am using the following code.
NSError *error1;
NSString *jsonString1;
NSData *jsonData1 = [NSJSONSerialization dataWithJSONObject:dictjson1
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData1) {
NSLog(#"Got an error: %#", error1);
} else {
jsonString1 = [[NSString alloc] initWithData:jsonData1 encoding:NSUTF8StringEncoding];
NSLog(#"converted json string is %#",jsonString1);
}
Please advice.
JSON has two structures: objects and arrays. Arrays are indexed by integers, and ordered. Objects are indexed by strings, and unordered. You can't enforce order on JSON objects; it is implementation-dependent. If you need to access object attributes in a certain order, enumerate the keys in this order in an array.
Dictionary with two nested dictionary and then array and then dictionary again how can i write code generally for objective c ,iOS 8;
{
"brands": {
"Clinique": {
"Foundation": {
"Even Better Makeup SPF 15": {
"productName": "Even Better Makeup SPF 15",
"colors": [
{
"id": "30816",
"client_id": "1422956000sjdaC",
"product_id": "190",
"shade_name": "Alabaster",
"shade_code": "#F0C9AE",
"color_id": null,
"image_url": "",
"price": "",
"offer": "",
"created_by": "1422956000sjdaC",
"created_date": "2015-03-06",
"sku_id": "",
"product_web_url": "",
"brand_id": "Clinique",
"product_name": "Even Better Makeup SPF 15",
"makeup_type": "Foundation",
"color_family": "cool"
},
As I can see, your JSON response is sending Product details, so the
"brands": {
"Clinique": {
"Foundation": {
will remain same always, and the response below it will change according to products.
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *productsDict = [[jsonDictionary objectForKey:#"brands"] objectForKey:#"Clinique"] objectForKey:#"Foundation"];
NSArray *keys=[productsDict allKeys];
for (int i = 0; i < keys.count; i++) {
NSDictionary *prodSingle = [productsDict objectForKey:[NSString StringWithFormat:#"%#",keys[i]]];
}
Now you have prodSingle, use this to get data of each product.
Note: Untested, I'll provide a tested version if this doesn't work.
EDIT:
As you said only brands key will remain constant and other will change dynamically,
you should do this :
So what you can do is create a NSDictionary with jsonData(response data) like this:
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
create a dictionary of brands
NSDictionary *brandsDict = [jsonDictionary objectForKey:#"brands"];
Now get all the keys:
NSArray *keys= [brandsDict allKeys];
Now use each key you got in Array to fetch data accordingly.
I have a JSON array(dictionary?) of objects that are themselves an array. I need to find a value within one of these arrays so that I can compare it later. Part of my JSON data:
[
{
"Name": "Exhibitor",
"Url": "api/congress/exhibitor",
"ResourceType": "Data",
"LastMod": 1389106977
},
{
"Name": "Workshop",
"Url": "api/congress/workshop",
"ResourceType": "Data",
"LastMod": 1389106977
},
{
"Name": "Speaker",
"Url": "api/congress/Speaker",
"ResourceType": "Data",
"LastMod": 1389106977
},
]
My method receives a table name as a parameter and returns a time stamp. How would I receive the time stamp (1389106977) for the table "workshop" for example? This seems so simple but I cannot work it out for 'nested' arrays/dictionaries.
Thanks,
edit:
This is the my code with trojanfoe's added to it.
NSError* localError;
NSMutableArray *syncDataArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (syncDataArray)
{
NSNumber *lastMod = nil;
for (NSDictionary *dict in syncDataArray)
{
NSLog(#"current table is: %#", dict[#"Name"]);
if ([tableName isEqualToString:dict[#"Name"]])
{
lastMod = dict[#"LastMod"];
//break;
}
}
NSLog(#"LastMod = %#", lastMod);
}
else
{
NSLog(#"syncDataArray is empty");
}
This works perfectly and makes sense
The JSON data looks like an array of dictionaries, so you can iterate over the array and test for the "Name" entry:
NSArray *jsonData = ...; // You have converted JSON to Objective-C objects already
NSNumber *lastMod = nul;
for (NSDictionary *dict in jsonData) {
if ([#"Workshop" isEqualToString:dict[#"Name"]]) {
lastMod = dict[#"LastMod"];
break;
}
}
if (lastMod) {
// You found it
}
(Note I am not certain the type of object used to store the "LastMod" object, so you might need to do some debugging to find out).
EDIT If you make extensive use of this data you should immediately convert the JSON data into an array of (custom) model objects, which will make it easier to manipulate the data as your app becomes more complex.
You have an array for dictionaries so it would look something like :
NSNumber *timestamp = [[JSON objectAtIndex:index] objectForKey:#"LastMod"];
NSNumber *timestamp = response[1][#"LastMod"];
I'll keep this brief. I'm using this code to parse JSON from a local file into an array of objects:
-(void)populateData
{
NSString* sourcePath = [[NSBundle mainBundle]pathForResource:#"ships" ofType:#"json"];
//get json string
NSString* JSONData = [[NSString alloc] initWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
NSData* data = [JSONData dataUsingEncoding:NSUTF8StringEncoding];
//put json in array
ships = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(#"%#", ships);
}
(Note: only showed one for the sake of brevity, there's ~20 entries in each one)
This method works for JSON formatted like this:
[
{
"name": "Santa Maria",
"operator": "Kingdom of Spain",
"flag": "flag_spain"
}
]
It returns null for JSON formatted like this:
[
{
"name": "Santa Maria",
"operator": "Kingdom of Spain",
"flag": "flag_spain",
"launched": "November 19, 1890",
"fate": "Destroyed in Havana, Cuba in Feburary 1898."
"cost":"$4,677,788.75",
"image": "maine_img",
"image_attribution": "Image is in the public domain."
}]
I haven't the faintest idea of why the smaller one works while the larger one doesn't. Any help would be appreciated.
There is a comma missing after:
"fate": "Destroyed in Havana, Cuba in Feburary 1898."
It's due to a syntax error on this line:
"fate": "Destroyed in Havana, Cuba in Feburary 1898."
(missing comma at the end)
There are tools to spot this kind of errors. For instance, http://jsonlint.com. This one seems to have better error messages http://jsonformatter.curiousconcept.com/.
And excuse me, but converting from NSData to NSString and then back to NSData is just pointless. Just call dataWithContensOfFile: and be done with it.