How to parse following JSON string in Objective-C - iphone

I have JSON string of following format, which need to be parsed in objective-c
[{"image":"<link_to_image>",
"pic-number":"2862",
"n-lines":5,
"default":"[{"line_1":"one!"},
{"line_2":"two"},
{"line_3":"three"},
{"line_4":"four"},
{"line_5":"five"}]"
},
... ]
The length of default array may vary for each item.

Try TouchJSON which is available in:
http://code.google.com/p/touchcode/
And also go through these
How to use touch json and sbjson
how to do json parsing in iphone
How to parse JSON into Objective C - SBJSON

Just found the cause of the problem,
Instead
"default":**"**[{"line_1":"one!"},
{"line_2":"two"},
{"line_3":"three"},
{"line_4":"four"},
{"line_5":"five"}]**"**
The JSON string should has following format otherwise the parsing will fail
"default":[{"line_1":"one!"},
{"line_2":"two"},
{"line_3":"three"},
{"line_4":"four"},
{"line_5":"five"}]

Related

how to use json_extract_path_text?

I am facing an issue with JSON extract using JSON_EXTRACT_PATH_TEXT in redshift
I have two separate JSON columns
One containing the modems the customer is using and the other one containing the recharge details
{"Mnfcr": "Technicolor","Model_Name":"Technicolor ABC1243","Smart_Modem":"Y"}
For the above, I have no issue extracting the Model_name using JSON_EXTRACT_PATH_TEXT(COLUMN_NAME, 'Model_Name') as model_name
[{"Date":"2021-12-24 21:42:01","Amt":50.00}]
This one is causing me trouble. I used the same method above and it did not work. it gave me the below error
ERROR: JSON parsing error Detail: ----------------------------------------------- error: JSON parsing error code: 8001 context: invalid json object [{"Date":"2021-07-03 17:12:16","Amt":50.00
Can I please get assistance on how to extract this using the json_extract_path_text?
One other method I have found and it worked was to use regexp_substring.
This second string is a json array (square braces), not an object (curly brackets). The array contains a single element which is an object. So you need to extract the object from the array before using JSON_EXTRACT_PATH_TEXT().
The junction for this is JSON_EXTRACT_ARRAY_ELEMENT_TEXT().
Putting this all together we get:
JSON_EXTRACT_PATH_TEXT(
JSON_EXTRACT_ARRAY_ELEMENT_TEXT( <column>, 0)
, 'Amt')
you can use json_extract_path_text like below example
json_extract_path_text(json_columnName, json_keyName) = compareValue
for more you can refer this article
https://docs.aws.amazon.com/redshift/latest/dg/JSON_EXTRACT_PATH_TEXT.html

How can I parse HTML?

Let's say I have the following string:
NSString * str = "<a href="http://www.google.com"style="">
</br>
Come onhello world"
How can I parse out the value of the anchor tag--"Come on"?
Should I use HTMLNode.h and HTMLParser.h? Examples would be appreciated.
If it is static, then you can use string manipulation to take out the substring out of the tag
OR
If it is dynamic and html/xml code keeps changing then you can use NSXMLParser. Keep in mind that NSXMLParser should more preferably used only when data is in XML format.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html
Hope this helps.

Parsing question

I would like to use cCSVParse http://michael.stapelberg.de/cCSVParse
in my project. I receive csv data from internet and want to parse it and save to core data. cCSVParse seems to be appropriate class of it. But it can only read csv data from file. When I receive data from internet, I wouldn't like to save it to file. Is there any way to use it for parsing data from NSData or NSString?
One approach is to surround the CSV data (one row at a time) with [] chars and parse it with a JSON parser. (But note that this only works if the non-numeric data items are enclosed in quotes.)
Or you can simply use
NSArray* items = [csvRow componentsSeparatedByString:#","];
if the data items aren't enclosed in quotes.

Problem with libxml2 and Unicode char in XML Soap with Qotes (") and &quot;

I'm using libxml2 for SOAP-Actions in an iPhone App.
The big problem is that i want send an SQL-Statement with
NSString *query = #"SELECT test FROM database WHERE test = \"string\""
But libxml2 converts the qotes " into &quot;
Any ideas how to prevent this?
Quotations within the texual data of XML are supposed to be converted to ", per the XML specification. The receiver is responsible for converting " back to quotations when extracting the text from the XML.

Parsing XML, details and attributes, iphone

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.