[futurebasic] Re: [FB] Searching for NSString in NSMutableString

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2016 : Group Archive : Group : All Groups

From: Steve Crossman <mactech@...>
Date: Sat, 23 Jul 2016 08:40:35 -0400
Hi Ken,

Thanks very much for this Ken.  I am going to try it out today.

I wasn’t aware of the app permissions under  macOS 10.12   I didn’t try any of my cloud sync code in macOS yet.  Guess what I am going to do next?

regards
Steve


> On Jul 23, 2016, at 7:51 AM, Ken Shmidheiser <kshmidheiser@...> wrote:
> 
> Steve asked:
> 
>> Anyone have a C code JSON parser ?
> 
> 
> Steve,
> 
> Here is a crude one based on your example. I have also included a function to parse JSON data from a URL. This works fine on OS X 10.7.5, but won't work on OS X 11.5 without info.plist modifications to give the app permission to access the internet, so I have commented it out. (Where is the Apple we used to love?)
> 
> Ken
> 
> 
> include "NSLog.incl"  
> include "ConsoleWindow"
> 
> local fn ParseJSONString( jsonString as CFStringRef ) as CFStringRef
> '~'1
> dim as CFMutableStringRef parsedStr
> dim as CFStringRef resultStr
> 
> parsedStr = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )
> 
> BeginCCode
> NSData *jsonData = [(NSString *)jsonString dataUsingEncoding:NSUTF8StringEncoding];
> 
> if( jsonData != nil )
> {
>     NSError *error = nil;
>     id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error];
> 
>     if (error == nil)
>     {
>          if ( [jsonObject isKindOfClass:[NSArray class]] )
>          {
>               NSMutableArray *jsonArray = (NSMutableArray *)jsonObject;
> 
>               if (!jsonArray)
>               {
>                      NSLog(@"Error parsing JSON");
>               }
>               else
>               {
>                    CFStringAppend( parsedStr, (CFStringRef)@"JSON object is an array:\n\n");
>                    int i = 0;
> 
>                    for( NSDictionary *item in jsonArray )
>                    {
>                         for (id element in item)
>                         {
>                             NSString *temp = [NSString stringWithFormat:@"Item: %d Key: %@\nValue: %@\n", i, [element description], [item valueForKey:[element description]]];
>                             CFStringAppend( parsedStr, (CFStringRef)temp);
>                         }
>                      i++;
>                      CFStringAppend( parsedStr, (CFStringRef)@"\n" );
>                    }
>               }
>          }
>          else
>          {    
>               CFStringAppend( parsedStr, (CFStringRef)@"JSON object is a dictionary with these Keys and Values:\n\n");
>               NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
>               for (id element in jsonDictionary)
>               {
>                   NSString *temp = [NSString stringWithFormat:@"Key: %@\nValue: %@\n\n", [element description], [jsonDictionary valueForKey:[element description]]];
>                   CFStringAppend( parsedStr, (CFStringRef)temp);
>               }
>          }
>     }
>     else
>     {
>          NSLog( @"JSON error: %@", error );
>     }
> }
> EndC
> resultStr = fn CFStringCreateCopy( _kCFAllocatorDefault, (CFStringRef)parsedStr )
> CFRelease( parsedStr )
> end fn = resultStr
> 
> 
> local fn ParseJSONfromURL( urlString as CFStringRef ) as CFStringRef
> '~'1
> dim as CFMutableStringRef parsedStr
> dim as CFStringRef resultStr
> 
> parsedStr = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )
> 
> BeginCCode
> NSURL *url            = [[NSURL alloc]initWithString:(NSString *)urlString];
> NSURLRequest *request = [NSURLRequest requestWithURL:url];
> [url release];
> NSURLResponse *response;
> NSData *jsonData      = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
> 
> if( jsonData != nil )
> {
>     NSError *error = nil;
>     NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:&error];
> 
>     if (error == nil)
>     {
>          for (id element in jsonDictionary)
>           {
>               NSString *temp = [NSString stringWithFormat:@"Key: %@\nValue: %@\n\n", [element description], [jsonDictionary valueForKey:[element description]]];
>               CFStringAppend( parsedStr, (CFStringRef)temp);
>           }
>     }
>     else
>     {
>          NSLog( @"JSON error: %@", error );
>     }
> }
> EndC
> resultStr = fn CFStringCreateCopy( _kCFAllocatorDefault, (CFStringRef)parsedStr )
> CFRelease( parsedStr )
> end fn = resultStr
> 
> 
> window _FBConsoleWndNum, @"Simple JSON Parser", (5, 50)-(700, 800)
> 
> dim as CFStringRef jsonString, parsedString
> dim as CFMutableStringRef mutStr
> 
> mutStr = fn CFStringCreateMutable( _kCFAllocatorDefault, 0 )
> 
> BeginCCode
> jsonString = (CFStringRef)@"{\"objectId\":\"EBY5AplCtH\",\"createdAt\":\"2016-07-22T12:39:58.579Z\"}";
> EndC
> parsedString = fn ParseJSONString( jsonString )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> 
> BeginCCode
> jsonString = (CFStringRef)@"[{\"success\": {\"objectId\":\"EBY5AplCtH\",\"createdAt\":\"2016-07-22T12:39:58.579Z\"}},{\"success\":{\"objectId\":\"6ygBpcp6Ab\",\"createdA\":\"2016-07-22T12:39:58.579Z\"}}]";
> EndC
> parsedString = fn ParseJSONString( jsonString )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> 
> // Steve's example JSON string
> BeginCCode
> CFStringAppend( mutStr, (CFStringRef)@"{\"results\":[{\"objectId\":\"oDCYCPyJeH\",\"stockItemID\":");
> CFStringAppend( mutStr, (CFStringRef)@"\"ITEM1^619\",\"locItemID\":\"ITEM1\",\"locStock\":273,\"locStart\":0,");
> CFStringAppend( mutStr, (CFStringRef)@"\"locName\":\"Macquarie\",\"locClient\":\"AMP Capital\",\"createdAt\":");
> CFStringAppend( mutStr, (CFStringRef)@"\"2016-07-03T17:59:09.341Z\",\"updatedAt\":\"2016-07-22T12:19:35.671Z\",");
> CFStringAppend( mutStr, (CFStringRef)@"\"locDesc\":\"Item 1 Description\",\"locID\":\"619\",\"archive\":\"0\",\"locSold\":46,\"locComm\":\"\"}]}");;
> EndC
> parsedString = fn ParseJSONString( (CFStringRef)mutStr )
> CFRelease( mutStr )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> 
> /* Here are a few online sites that offer JSON tests. For these to work on newer Macs,
> you will have to change the permissions in your application's info.plist. This has
> been addressed in other threads on the list.
> 
> fn ConsolePrintCFType( @"This computer's IP address from http://ip.jsontest.com/:" )
> parsedString = fn ParseJSONfromURL( @"http://ip.jsontest.com/" )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> 
> fn ConsolePrintCFType( @"Date and time from http://date.jsontest.com/:" )
> parsedString = fn ParseJSONfromURL( @"http://date.jsontest.com/" )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> 
> fn ConsolePrintCFType( @"Random date trivia from http://numbersapi.com/random/date?json:" )
> parsedString = fn ParseJSONfromURL( @"http://numbersapi.com/random/date?json" )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> 
> fn ConsolePrintCFType( @"Your HTTP headers sent to server echoed from http://headers.jsontest.com/:" )
> parsedString = fn ParseJSONfromURL( @"http://headers.jsontest.com/" )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> 
> fn ConsolePrintCFType( @"Some Star Wars fun:" )
> parsedString = fn ParseJSONfromURL( @"http://swapi.co/api/people/1/"    )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> 
> parsedString = fn ParseJSONfromURL( @"http://swapi.co/api/planets/3/"   )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> 
> parsedString = fn ParseJSONfromURL( @"http://swapi.co/api/starships/9/" )
> fn ConsolePrintCFType( parsedString )
> CFRelease( parsedString )
> */
> 
> RunApplicationEventLoop()
> 
>