Reading JSON from a .NET .asmx Web Service

227 views
Skip to first unread message

Timothy Taylor

unread,
Oct 18, 2009, 3:27:46 PM10/18/09
to iphonesdkd...@googlegroups.com
Does anyone here have any experience or tips on how to read an ASMX .NET (C# or VB .NET) Web Service that outputs JSON format?

I have the Web Service grabbing from a MSSQL database and outputting JSON, but now I'm just trying to figure out how to get my iPhone app to read it.  I've tried a couple of tutorials online and haven't had much luck.

It doesn't seem to be very common that people are accessing MSSQL databases remotely from their iPhone apps, does it?

Any help you can give would be much appreciated!  Sample Code would be even better. Thanks in advance,

________________
Tim Taylor


lzbob

unread,
Oct 19, 2009, 12:34:40 PM10/19/09
to iphonesdkd...@googlegroups.com
I am using this:

http://code.google.com/p/json-framework/

Easy to install and use, enjoy

-z

marcantonio

unread,
Oct 19, 2009, 12:48:01 PM10/19/09
to iphonesdkd...@googlegroups.com
Timothy,

>Does anyone here have any experience or tips on
>how to read an ASMX .NET (C# or VB .NET) Web 
>Service that outputs JSON format?

lzbob suggests:
I totally agree. It works beautifully; I've been using it to access a json webservice, though I can't remember whether it was .NET-generated or not.

Also, see the following article:

* Consuming a RESTful Service (bit.ly) in an iPhone Application

..it deals with generic XML and JSON.


--
Marcantonio (Marco) Magnarapa



offwhite

unread,
Oct 19, 2009, 12:54:36 PM10/19/09
to iPhone SDK Development
Tim,

I got your question via Romke. I have been using ASP.NET MVC to send
JSON data to use in an iPhone app. I use ASP.NET MVC versus ASMX
because it can act as a RESTful with very little work. You an see a
live sample here.

http://services.mygroupsapp.com/Data/

And you can get all of the source that runs the site here.

http://code.google.com/p/mwiphonesdk/source/browse/#svn/trunk/iMADE/Website

This is done to specifically make the data available in a platform
neutral way. An Android, Palm Pre or Blackberry app could also access
this data with ease. A .NET app could as well.

The data controller shows exactly how to do it and you will be happy
with how little code is involved.

http://code.google.com/p/mwiphonesdk/source/browse/trunk/iMADE/Website/iMade.Web.MVC/Controllers/DataController.cs

The objects that return by passing to the Json method (which
serializes the object to JSON) are specifically value objects which I
populate with the data model. In these data controller methods I am
converting XML to JSON in 3 cases while one is converting an iCalendar
object to a simpler value object for use on the View in the iPhone
app. Offloading all this heavy lifting to the server in C# has cut
down on the work in Objective-C and from what I am reading, working
with JSON is much more memory efficient than XML.

You can get a good example of loading this data from this project.

http://code.google.com/p/mwiphonesdk/source/browse/trunk/iMADE/#iMADE/PrepTasks/01%20Basic%20JSON/PressThisButton

Good luck!

offwhite

unread,
Oct 19, 2009, 12:59:55 PM10/19/09
to iPhone SDK Development
I would go with TouchJSON over JSON-Framework because there are some
bugs in the JSON-Framework somehow related to the 3.1.2 release for
Snow Leopard. A friend of mine was using it but it stopped working for
him after Snow Leopard. He moved to TouchJSON and it works just fine.

It is also easy to change your code over to TouchJSON.

One thing I also noticed about loading data into an NSDictionary is
that if you try to load a JSON array that it will fail because it has
to start with a key, which is not how an array works. So I wrap it
with addition bits, like...

NSString *jsonString = [NSString stringWithFormat:@"{ \"json\" :
%@ }", dataString];

This creates a JavaScript object with "json" as the key that you would
access from the NSDictionary and then access the array below it.

Brennan
http://groups.google.com/group/mwiphonesdk
Reply all
Reply to author
Forward
0 new messages