JSON Encoding - Get MySQL data to an iPhone - php

Im new to JSON and have whats probably a basic question. I have some MySQL rows I want grab with Php, and then JSON encode for a response to an iphone request.
so the questions are:
1) Whats the format for the array to encode this info?
2) Where the does the response code (200) fit into it?

Try this tutorial. http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
Basically on the PHP side, write a script that fetches the MySQL data and then echo back the output of json_encode. The iPhone side will call to the URL of this script using the method above.
I would suggest you take a look at json.org and familiarize yourself with the JSON format.

1) On the Obj-c side of things, have you heard of Stig's JSON framework? https://github.com/stig/json-framework
I use it in my iPhone apps and it works very well for encoding and decoding JSON. Are you interested in the the format for the array to encode the information on the iPhone side or server side? You should create the array in the way that makes the most sense and then make sure the iPhone side is reading the array the same way the PHP side sends it.
2) The HTTP 200 response code simply means your request has succeeded. As opposed to like a 404 or something where the webpage doesn't exist.

Related

Can we use JSON body format when doing Opencart API

right now im learning about Opencart API. form opencart documentation Documentation every example write with json format.
however when i test it on postman. i test API for add product to cart. i can't put the json raw format body. i need to put the data on form-data.
this example result when i put raw json body format. and return empty array
and this result wheni put data on form-data
does opencart can't receive json format when do API call?
opencart documentation Documentation every example write with json format.
No, it doesn't.
The examples use a Python library and Python syntax.
The value of data looks superficially like JSON but is Python. The library takes that data and, according to the docs, converts it to form encoded data.
data – the body to attach to the request. If a dictionary or list of tuples [(key, value)] is provided, form-encoding will take place.
The API doesn't appear to support JSON.

How to pass field values from database hosted on a server to android app using php?

I have a database hosted on server whose field values have to be passed to my app. I would like to do with PHP. But people suggest me to use JSON too. Is JSON required? Please guide me how to pass the field values to android app.
JSON means JavaScript Object Notation and it's just a way of formatting your output in a standard way.
So, if you'd like to pass data from a database to an application, you'd need to implement a small API. This can be done using PHP. At this point, you can access data from your database using a browser and parametrizing your queries using url parameters.
PHP can render the data in a simple HTML table for example, but this is just a way of presenting your data. You can also use JSON.
This means that if you need the badges a user has earned, you'll use something like this:
<link_to_your_api>/index.php?method=getBadges&user=<user>
This in turn, will make a request to the database
<?php
// 1. connect to database
// 2. query for the information
// 3. get the result as array
$result = $db->getData();
echo json_encode($result);
?>
This is just an example, hope it helps.
link to json documentation: http://www.json.org/
You pass the data to your app, when it makes a request to your PHP script. JSON is handy because you can package your data in a format, that is both well readable by humans and machines. You can use the gson library then, to process the JSON data in your app.
Create REST api using any server side script PHP,nodejs or any you like which returns JSON response
call the rest api using http request which returns JSOn text
decode the JSON string to JSON object and use with your android code

encrypted object in mysql [duplicate]

The goal is to unserialize a PHP serialized string and get sutable object in C#
Is there any way to make this possible in C#(.Net)?
To be more specific:
We need to make an application which comunicates (Via HTTP) to specific website which returns the needed information. Fortunately/unfortunately we dont have permission to website so the data (array mostly) that is returned from website is PHP serialized.
I suppose using JSON as an intermediary step could be useful.
You should probably write it to XML or JSON. You can construct your C# object back from the XML
Edit: Looks like there is already a XML serializer for PHP

Using data from a web-service in an iOS Application

I have to list the table entries from mysql in my iPhone app when a button is pressed. I am able to do it properly but it is just not the way I want it. I am using a PHP script to send Xcode what has to be printed.
First, I tried doing it using HTML table, but I didn't like the way it was printed with cells. Next I tried printing plain text by giving spaces(between columns) and \n for every new row. I used NSURL and loaded the webView to the iPhone. Looks good on browser but the same is not preserved when the iPhone tries to open it.
Is there a good way to do this? So I can just list the table entries without having to go through the traditional HTML table or any other idea is welcome.
Also, please try to be easily understood, as I am new to Obj-C, and PHP as well.
Thanks!!
Any thoughts on how I can do this in a UITableView..?? Do I have to return a string with component separation characters and fill in the tableView?
Output the results encoded in JSON. Send an a(sync) request to the server using NSURLConnection or using a third-party library such as AFNetworking. Parse the JSON using NSJSONSerialization, turning the results into an array/dictionary depending on the contents. Then parse the results into the UITableViewCell. It may be easier to subclass the cell so that you include the data that you'd like to use.
To encode the results from the database into JSON, you can use the method json_encode().

shall i retrieve database records with JSON ?

i'm a little bit confused here. i've PHP file that retrieve database records . i'll call it with an Ajax call from my frontend . do i need to convert the records to JSON ? if no when do i need to do that
You don't "need" to return the results as JSON. But I would recommend it. JSON is very portable, so it will be easier for other applications to interact with your application. It's also much easier to parse JSON than it is records separated by simple delimiters.
For example, you can use Crockford's JSON parser: http://www.json.org/js.html
As for JSON vs XML: Why need to use JSON in php and AJAX
You don't have to use JSON but you can encode any associative array using the function json_encode:
http://php.net/manual/en/function.json-encode.php
If your client is requesting the data in JSON format, then it's probably best to take the results from your database call and convert them to a JSON-formatted string before returning it to your client.
But your client's AJAX call could also be requesting the data in XML format, too.
So the answer depends on what the client is expecting.

Categories