Skip to main content

Posts

Showing posts with the label json

How to format a JSON date?

I'm taking my first crack at Ajax with jQuery. I'm getting my data onto my page, but I'm having some trouble with the JSON data that is returned for Date data types. Basically, I'm getting a string back that looks like this:

What should a JSON service return on failure / error

I'm writing a JSON service in C# (.ashx file). On a successful request to the service I return some JSON data. If the request fails, either because an exception was thrown (e.g. database timeout) or because the request was wrong in some way (e.g. an ID that doesn't exist in the database was given as an argument) how should the service respond? What HTTP status codes are sensible, and should I return any data, if any?

JSON left out Infinity and NaN; JSON status in ECMAScript?

Any idea why JSON left out NaN and +/- Infinity? It puts Javascript in the strange situation where objects that would otherwise be serializable, are not, if they contain NaN or +/- infinity values. Looks like this has been cast in stone: see RFC4627 and ECMA-262 at the top of p. 197: Finite numbers are stringified as if by String(number). NaN and Infinity regardless of sign are represented as the string null. Source: Tips4all

ExtJS grab JSON result

I'm generating JSON response from PHP witch looks like this: { done:'1', options: [{ message:'Example message'},{message:'This is the 2nd example message'}]} I want to grab these results using ExtJS. This is what I have so far: Ext.Ajax.request({ loadMask: true, url: 'myfile.php', params: {id: "1"} }); What do I have to write next to get the json results like this: var mymessages = jsonData.options; And mymessages should contain Example message and This is the 2nd example message. Thank you. Source: Tips4all

Android JSON HttpClient to send data to PHP server with HttpResponse

I am currently trying to send some data from and Android application to a php server (both are controlled by me). There is alot of data collected on a form in the app, this is written to the database. This all works. In my main code, firstly I create a JSONObject (I have cut it down here for this example): JSONObject j = new JSONObject(); j.put("engineer", "me"); j.put("date", "today"); j.put("fuel", "full"); j.put("car", "mine"); j.put("distance", "miles"); Next I pass the object over for sending, and receive the response: String url = "http://www.server.com/thisfile.php"; HttpResponse re = HTTPPoster.doPost(url, j); String temp = EntityUtils.toString(re.getEntity()); if (temp.compareTo("SUCCESS")==0) { Toast.makeText(this, "Sending complete!", Toast.LENGTH_LONG).show(); } The HTTPPoster class: public static HttpResponse doPost(String url, JSO

Json-RPC Method from iOS to Android

I have a java webservice, with some method. The webservice methods are in this form: @Webservice(paramNames = {"email", "password", "stayLogged", "idClient"}, public Response startSession(String email, String password, Boolean stayLogged, String idClient) throws Exception { boolean rC = stayLogged != null && stayLogged.booleanValue(); UserService us = new UserService(); User u = us.getUsersernamePassword(email, password); if (u == null || u.getActive() != null && !u.getActive().booleanValue()) { return ErrorResponse.getAccessDenied(id, logger); } InfoSession is = null; String newKey = null; while (newKey == null) { newKey = UserService.md5(Math.random() + " " + new Date().getTime()); if (SessionManager.get(newKey) != null) { newKey = null; } else { is = new InfoSession(u, rC, newKey); if (idClient != null && id

Json-RPC Method from iOS to Android

I have a java webservice, with some method. The webservice methods are in this form: @Webservice(paramNames = {"email", "password", "stayLogged", "idClient"}, public Response startSession(String email, String password, Boolean stayLogged, String idClient) throws Exception { boolean rC = stayLogged != null && stayLogged.booleanValue(); UserService us = new UserService(); User u = us.getUsersernamePassword(email, password); if (u == null || u.getActive() != null && !u.getActive().booleanValue()) { return ErrorResponse.getAccessDenied(id, logger); } InfoSession is = null; String newKey = null; while (newKey == null) { newKey = UserService.md5(Math.random() + " " + new Date().getTime()); if (SessionManager.get(newKey) != null) { newKey = null; } else { is = new InfoSession(u, rC, newKey); if (idClient != null && id

Android: JSONObject cannot be converted to JSONArray

I want to get data from php file to Android using JSON. This is my code: .... HttpEntity e = r.getEntity(); String data = EntityUtils.toString(e); JSONArray timeline = new JSONArray(data); JSONObject last = timeline.getJSONObject(0); return last; When I debug the program there is JSONException on this line: JSONObject last = timeline.getJSONObject(0); data = {"a":1,"b":2,"c":3,"d":4,"e":5} and the Exception is: "org.json.JSONException: Value {"d":4,"e":5,"b":2,"c":3,"a":1} of type org.json.JSONObject cannot be converted to JSONArray"

Parsing JSON Feed iOS 5

I'm pretty new to iOS development, and I'm wanting to parse the values of a JSON twitter feed, so I can pull them through into a tableView. So far, I have the information from the feed logging into my console bar as a string, but I'm stuck with where to go next. My current code is: - (void)loadTweets { NSString *twitterURL = [NSString stringWithFormat:@"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=%d", tweetCellCount]; NSURL *fullURL = [NSURL URLWithString:twitterURL]; NSError *error = nil; NSData *dataURL = [NSData dataWithContentsOfURL:fullURL options:0 error:&error]; NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]; NSLog(@"Everything:\n%@", strResult); } Which gives me the raw data from the feed: [{"id_str":"171930825225551872","coordinates":null,"in_reply_to_status_id_str":null,"place":null,"po

gson deserialization with multiple FieldNamingPolicy

I have a JSON document returned to me from a third party that looks like this: { "data" : { "events" : [ { "Ages" : "", "AttendingCount" : 0 } ] } } i am attempting to deserialize this into Java objects using gson: Response EventCollection data EventCollection Collection events Event String ages; int attendingCount; gson is my preferred json parser at this stage of the project The json field names are in different formats. In the context of gson's FieldNamingPolicy Response.data could be parsed with FieldNamingPolicy.IDENTITY however the nested Event.ages field would need to use FieldNamingPolicy.UPPER_CAMEL_CASE Is there a way i can use multiple FieldNamingPolicy configs per gson.fromJson call? thanks

Why Json is null in this situation?

I'm trying to return a JSON encoded array from a PHP page via ajax. here's my code from the calling page $('#test_load').click(function(){ $.ajax({ type:"POST", url:'/actions/admin_load.php', dataType: 'json', asynch:false, data:"action=4", success: function(json) { alert("we're back"); alert(json.a); } }); }); here's my php code //return json array case "4" : $str2 = "HELLO"; $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); $str = json_encode($arr); echo $str; break; I keep getting "json is null". Any suggestions would be much appreciated