Android parse JSON with multiple array response from php
-
I recently started working with andorid. How to parse dynamic json with multiple arrays response from php server. Login Response { "status": 1, "data": { "msg": "LoggedIn", "user_id": "2" } } Login response with error message { "status": 0, "data": "No users found with given email." } and an other one Inventory List { "status": 1, "data": [ { "inventory_id": "33", "apron_id": "123456", "nickname": "uyi", "location": "13", "manufacture": "0", "garment_type": "yuyh", "color": "juki", "core_material": "ytyt", "Date_purchase": "2015-04-10", "UID_no": "ikujki", "serial": "iui", "Batch": "ikk", "Expiration": "2015-04-23", "QTY": "898", "apron_status": "0", "apron_retire": "0", "created_user": "2", "created_time": "2015-04-10 05:22:38", "update_time": "2015-04-10 05:22:38" }, { "inventory_id": "32", "apron_id": "12345mn", "nickname": "gfhgh", "location": "12", "manufacture": "0", "garment_type": "hgjyhj", "color": "ytgtfghtg", "core_material": "fhgfhy", "Date_purchase": "2015-04-28", "UID_no": "rtryttttttttt", "serial": "hfh", "Batch": "rtrrtyy", "Expiration": "2015-03-17", "QTY": "7688", "apron_status": "0", "apron_retire": "0", "created_user": "2", "created_time": "2015-04-10 05:15:54", "update_time": "2015-04-10 05:15:54" } ] } Thanks in advance.
-
Answer:
to get login response you can do like this JSONObject jobj=new JSONObject(result.toString()); String status=jobj.getString("status"); if(status.equalsIgnoreCase("1")) { //login success JSONObject Jdata=jobj.getJSONObject("data"); String Message=Jdata.getString("msg"); String UserId=Jdata.getString("user_id"); } else { //failure } and for Inventory List you can do like this JSONObject jobj=new JSONObject(result.toString()); JSONArray arrData=jobj.getJSONArray("data"); for (int i = 0; i < arrData.length(); i++) { JSONObject jdata=arrData.getJSONObject(i); //here u can get all field like this String nickname=jdata.getString("nickname"); }
manikanta g at Stack Overflow Visit the source
Other answers
Investigate the http://developer.android.com/reference/org/json/JSONObject.html and http://developer.android.com/reference/org/json/JSONArray.html classes. You can parse extremely simply: JSONObject json = new JSONObject(jsonString); int status = json.getInt("status"); Using both an Array and an Object for your "data" key will make things irritating for you. You should think about using a different key, or another key that will dictate to you what you are reading.
Knossos
Related Q & A:
- Is it possible for android emulator to send multiple SMS?Best solution by developer.android.com
- How to parse JSON from String?Best solution by Stack Overflow
- Android: How to parse JSON file with Gson Library?Best solution by Stack Overflow
- How do I parse nested JSON object?Best solution by Stack Overflow
- How to parse SOAP response with PHP?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.