1

I have generated JSON data from Java Restful WebServices and I need to put into the Objective C code. How can I use the JSON data and integrate into Objective C? The IDE has generated the local URL, how can I use the generated JSON data in other machine. Thank you

Chhewang
  • 9
  • 5
  • A point worth mentioning, as long your web service produces valid JSON, there should not be any need to worry about whether the server is executing Java code, C#, node.js, etc. – Mike D Jul 25 '13 at 16:44
  • Thank you for your response. The only problem is that how to call the generated JSON data in to Objective C. Since I work on the JAVA and my co-worker is working on Objective C. We have problem of implementing in Objective C. I work on PC and he is working in Mac. – Chhewang Jul 25 '13 at 18:24

3 Answers3

1

Use any of the many JSON parsers available. This question compares a few of them: Comparison of JSON Parser for Objective-C (JSON Framework, YAJL, TouchJSON, etc)

Community
  • 1
  • 1
Nuoji
  • 3,438
  • 2
  • 21
  • 35
1

Have a look at NSURLConnection to retrieve the JSON from your web service. Then you can make use of NSJSONSerialization to parse it.

Paul Dardeau
  • 2,589
  • 1
  • 13
  • 10
1

You can request the NSData from the URL and then use NSJSONSerialization to interpret it. For example:

NSURL *url = [NSURL URLWithString:@"http://www.put.your.url.here/test.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (error) {
        NSLog(@"%s: sendAsynchronousRequest error: %@", __FUNCTION__, error);
        return;
    }

    NSError *jsonError = nil;
    NSArray *results = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
    if (jsonError) {
        NSLog(@"%s: JSONObjectWithData error: %@", __FUNCTION__, jsonError);
        return;
    }

    // now you can use the array/dictionary you got from JSONObjectWithData; I'll just log it

    NSLog(@"results = %@", results);
}];

Clearly, that assumed that the JSON represented an array. If it was a dictionary, you'd replace the NSArray reference with a NSDictionary reference. But hopefully this illustrates the idea.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I have used restful webservice and my program it in login.java however it generates the json data. Does http://www.put.your.url.here is the my localhost or I need to host in my server? I don't have test.json. My one is in Java. Can you please make me clear? Thank you.. – Chhewang Jul 25 '13 at 16:27
  • @Chhewang The URL should just be whatever host is serving up your web service. If that web service is currently running on the same computer you're running your app on the simulator, you can be able to get away with `localhost`, but it might be more prudent to use some hostname that your local network will resolve to your development machine so that you can also test on devices connected to your wifi network. – Rob Jul 25 '13 at 16:53
  • @Chhewang If this web service is running on your Mac, you can go to "System Preferences" - "Sharing" and it will show you what your computer's name is on your local network. E.g. Mine says "Firstname-Lastname's-iMac.local", so my devices on my wifi network can access it via `"http://Firstname-Lastname's-iMac.local/sample.json"`, or whatever. That way, devices on my wifi network can access my Macs on my local network. – Rob Jul 25 '13 at 16:54
  • Thank you very much Rob. I am still working on it. When I am done I will let you know. – Chhewang Jul 25 '13 at 18:12
  • Hi Rob, I still can't connect from my remote computer. I used the path as you mentioned. Do you have any other suggestions. Thank you... – Chhewang Jul 29 '13 at 13:43
  • @Chhewang You can use the IP number for the computer running your server. So, if the device is connecting via wifi (not cellular) on your local network, you can use that number (usually in the form of 192.168.1.x), e.g. `http://192.168.1.43/test.json`. Also test your URL using another computer (iOS's Safari), and ensure that the URL resolves correctly on your LAN. Or, if testing on simulator and running on the same machine as web service, use `localhost`. Clearly, as you go to a production, you need something accessible over the internet, but for testing, you can access the URL via your LAN. – Rob Jul 29 '13 at 20:06
  • thank you Rob. I figure out this morning. I am using Netbeans IDE and how can run my application forever. So, I can access from my client any time. I mean if I close my IDE the service is down. How can I cope with this kind of challenges? Like PHP code we can host in XAMPP, we can use any time and any where. There are ways we can do with JAVA. I know there are but unable to find help. – Chhewang Jul 29 '13 at 20:15
  • @Chhewang The question is what's going to run your web service. Often I think people will use Tomcat. I think if you google "Mac OS X install tomcat 7" and you'll get hits [like this](http://wolfpaulus.com/jounal/mac/tomcat7). If it's not self evident, I'd suggest you post a new question (if searching for answers is unsuccessful) about installing tomcat or some other java web server on your platform of choice. We're now beyond the scope of an Objective-C question about JSON, so a new question on the new topic (only after searching for existing questions/answers) is appropriate. – Rob Jul 29 '13 at 21:00