SQLite
 sql >> डेटाबेस >  >> RDS >> SQLite

कैसे दो Json प्रतिक्रिया प्राप्त करने के लिए Json वस्तु और सरणी

नोट :जैसा कि पिछले प्रश्न में बताया गया है, दी गई JSON स्ट्रिंग को सही प्रारूप में होना चाहिए यानी या तो प्रत्येक ऑब्जेक्ट (लॉगिन, अकाउंट) के लिए एक कुंजी होनी चाहिए या उन्हें एक ही सरणी (इनपुट) में रखना चाहिए। मैं दोनों विकल्पों का समाधान दे रहा हूं।

नौसिखिया, मैं आपको 2 अलग-अलग तरीके प्रदान कर रहा हूं ताकि आप आने वाली JSON स्ट्रिंग को संभाल सकें, इस पर निर्भर करते हुए कि आप इसे कैसे बनाते हैं या तो सिंगल JSON स्ट्रिंग में 2 ऑब्जेक्ट या JSON सरणी में 2 ऑब्जेक्ट।

आप अपना समाधान चुन सकते हैं :)

कोड आज़माएं, अगर आपको और मदद चाहिए तो मुझे बताएं और जवाब स्वीकार करें।

OPTION1 :सिंगल JSON स्ट्रिंग में 2 ऑब्जेक्ट

{
   "login":{
      "error":false,
      "user":{
         "br_code":12,
         "mem_id":13,
         "username":"novalyn",
         "email":"[email protected]",
         "created_at":"2016-07-22 09:05:21"
      }
   },
   "accounts":{
      "error":false,
      "sl_summ":[
         {
            "sl_desc":"PA : Savings Account",
            "tr_date":"2015-08-17",
            "actual_balance":"483.67",
            "available_balance":"483.67"
         },
         {
            "sl_desc":"PA : Savings - Cash Bond",
            "tr_date":"2015-08-28",
            "actual_balance":"10129.43",
            "available_balance":"10129.43"
         }
      ]
   }
}

OPTION2 :एकल JSON सरणी स्ट्रिंग में 2 ऑब्जेक्ट

{
   "input":[
      {
         "error":false,
         "user":{
            "br_code":12,
            "mem_id":13,
            "username":"novalyn",
            "email":"[email protected]",
            "created_at":"2016-07-22 09:05:21"
         }
      },
      {
         "error":false,
         "sl_summ":[
            {
               "sl_desc":"PA : Savings Account",
               "tr_date":"2015-08-17",
               "actual_balance":"483.67",
               "available_balance":"483.67"
            },
            {
               "sl_desc":"PA : Savings - Cash Bond",
               "tr_date":"2015-08-28",
               "actual_balance":"10129.43",
               "available_balance":"10129.43"
            }
         ]
      }
   ]
}

JSON स्ट्रिंग के दोनों परिदृश्यों (OPTION1 और OPTION2) को संभालने के लिए कोड

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public static void jsonExample() {
    // OPTION 1
    String twoObjectString = "{ \"login\":{ \"error\":false, \"user\":{ \"br_code\":12, \"mem_id\":13, \"username\":\"novalyn\", \"email\":\"[email protected]\", \"created_at\":\"2016-07-22 09:05:21\" } }, \"accounts\":{ \"error\":false, \"sl_summ\":[ { \"sl_desc\":\"PA : Savings Account\", \"tr_date\":\"2015-08-17\", \"actual_balance\":\"483.67\", \"available_balance\":\"483.67\" }, { \"sl_desc\":\"PA : Savings - Cash Bond\", \"tr_date\":\"2015-08-28\", \"actual_balance\":\"10129.43\", \"available_balance\":\"10129.43\" } ] } }\n";
    // OPTION 2
    String arrayString = "{ \"input\": [ { \"error\":false, \"user\":{ \"br_code\":12, \"mem_id\":13, \"username\":\"novalyn\", \"email\":\"[email protected]\", \"created_at\":\"2016-07-22 09:05:21\" } }, { \"error\":false, \"sl_summ\":[ { \"sl_desc\":\"PA : Savings Account\", \"tr_date\":\"2015-08-17\", \"actual_balance\":\"483.67\", \"available_balance\":\"483.67\" }, { \"sl_desc\":\"PA : Savings - Cash Bond\", \"tr_date\":\"2015-08-28\", \"actual_balance\":\"10129.43\", \"available_balance\":\"10129.43\" } ] } ] }\n";
    try {
        Log.d("TEST", "COMBINED 2 OBJECTS             ");
        Log.d("TEST", "INPUT String                 : " + twoObjectString);
        JSONObject twoJSONObjects = new JSONObject(twoObjectString);
        handleTwoObjects(twoJSONObjects);

        Log.d("TEST", "2 OBJECTS IN ARRAY             ");
        Log.d("TEST", "INPUT String                   " + arrayString);
        JSONObject arrayJSONObject = new JSONObject(arrayString);
        handleArrayOfObjects(arrayJSONObject);
    } catch (Exception exception) {
        Log.d("TEST", exception.toString());
    }
}

// OPTION 1
public static void handleTwoObjects(JSONObject jsonObject)  throws Exception {
    // read the json string into a json object
    Log.d("TEST", "JSON String                  : " + jsonObject.toString());

    if (!jsonObject.isNull("login")) {
        JSONObject loginObject = (JSONObject) jsonObject.get("login");

        // access individual json object thru jsonObject.get("FIELD_NAME")
        Log.d("TEST", "-error attribute             : " + loginObject.get("error").toString());

        // Check if its login data i.e. user present
        if (!loginObject.isNull("user")) {
            // handle user login data
            JSONObject userJSONObject = (JSONObject) loginObject.get("user");
            Log.d("TEST", "User                         : " + userJSONObject.toString());
            Log.d("TEST", "-br_code attribute           : " + userJSONObject.get("br_code").toString());
            Log.d("TEST", "-mem_id attribute            : " + userJSONObject.get("mem_id").toString());
            Log.d("TEST", "-username attribute          : " + userJSONObject.get("username").toString());
            Log.d("TEST", "-email attribute             : " + userJSONObject.get("email").toString());
            Log.d("TEST", "-created_at attribute        : " + userJSONObject.get("created_at").toString());
            // Check if its account data i.e. sl_summ is present
        } else {
            // a new JSON string that doesn't have user in login Object
            Log.d("TEST", "Unknown JSON String          : " + loginObject.toString());
        }
    }

    if (!jsonObject.isNull("accounts")) {
        JSONObject accountsObject = (JSONObject) jsonObject.get("accounts");

        // access individual json object thru jsonObject.get("FIELD_NAME")
        Log.d("TEST", "-error attribute             : " + accountsObject.get("error").toString());

        JSONArray slArray = accountsObject.optJSONArray("sl_summ");

        // Check if its login data i.e. user present
        if (slArray != null) {
            // handle account data
            JSONArray array = ((JSONArray)accountsObject.getJSONArray("sl_summ"));
            // access individual json array thru jsonObject.getJSONArray("FIELD_NAME")
            Log.d("TEST", "-sl_summ array               : " + accountsObject.getJSONArray("sl_summ").toString());
            for (int index=0; index<array.length(); index++) {
                JSONObject object = (JSONObject)array.get(index);
                Log.d("TEST", "-sl_desc attribute           : " + object.get("sl_desc").toString());
                Log.d("TEST", "-tr_date attribute           : " + object.get("tr_date").toString());
                Log.d("TEST", "-actual_balance attribute    : " + object.get("actual_balance").toString());
                Log.d("TEST", "-available_balance attribute : " + object.get("available_balance").toString());
                Log.d("TEST", "---------------------------------");
            }
        } else {
            // a new JSON string that doesn't have sl_summ as member variable so display it and write new handler code
            Log.d("TEST", "Unknown JSON String          : " + jsonObject.toString());
        }
    }
}

// OPTION 2
public static void handleArrayOfObjects(JSONObject jsonObject)  throws Exception {
    // read the json string into a json object
    Log.d("TEST", "JSON String                  : " + jsonObject.toString());

    JSONArray inputArray = jsonObject.optJSONArray("input");

    if (inputArray != null && inputArray.length() > 0) {
        for (int oindex = 0; oindex < inputArray.length(); oindex++) {

            JSONObject currentObject = (JSONObject) inputArray.get(oindex);

            JSONArray slArray = currentObject.optJSONArray("sl_summ");

            // access individual json object thru jsonObject.get("FIELD_NAME")
            Log.d("TEST", "-error attribute             : " + currentObject.get("error").toString());

            // Check if its login data i.e. user present
            if (!currentObject.isNull("user") && slArray == null) {
                // handle user login data
                JSONObject userJSONObject = (JSONObject) currentObject.get("user");
                Log.d("TEST", "User                         : " + userJSONObject.toString());
                Log.d("TEST", "-br_code attribute           : " + userJSONObject.get("br_code").toString());
                Log.d("TEST", "-mem_id attribute            : " + userJSONObject.get("mem_id").toString());
                Log.d("TEST", "-username attribute          : " + userJSONObject.get("username").toString());
                Log.d("TEST", "-email attribute             : " + userJSONObject.get("email").toString());
                Log.d("TEST", "-created_at attribute        : " + userJSONObject.get("created_at").toString());
                // Check if its account data i.e. sl_summ is present
            } else if (slArray != null && currentObject.isNull("user")) {
                // handle account data
                JSONArray array = ((JSONArray)currentObject.getJSONArray("sl_summ"));
                // access individual json array thru jsonObject.getJSONArray("FIELD_NAME")
                Log.d("TEST", "-sl_summ array               : " + currentObject.getJSONArray("sl_summ").toString());
                for (int index=0; index<array.length(); index++) {
                    JSONObject object = (JSONObject)array.get(index);
                    Log.d("TEST", "-sl_desc attribute           : " + object.get("sl_desc").toString());
                    Log.d("TEST", "-tr_date attribute           : " + object.get("tr_date").toString());
                    Log.d("TEST", "-actual_balance attribute    : " + object.get("actual_balance").toString());
                    Log.d("TEST", "-available_balance attribute : " + object.get("available_balance").toString());
                    Log.d("TEST", "---------------------------------");
                }
            } else {
                // a new JSON string that doesn't have user or sl_summ as member variable so display it and write new handler code
                Log.d("TEST", "Unknown JSON String          : " + jsonObject.toString());
            }
        }
    }
}

OPTION1 और OPTION2 के लिए नमूना लॉग

07-05 20:21:58.001 8178-8178/? D/TEST: COMBINED 2 OBJECTS             
07-05 20:21:58.001 8178-8178/? D/TEST: INPUT String                 : { "login":{ "error":false, "user":{ "br_code":12, "mem_id":13, "username":"novalyn", "email":"[email protected]", "created_at":"2016-07-22 09:05:21" } }, "accounts":{ "error":false, "sl_summ":[ { "sl_desc":"PA : Savings Account", "tr_date":"2015-08-17", "actual_balance":"483.67", "available_balance":"483.67" }, { "sl_desc":"PA : Savings - Cash Bond", "tr_date":"2015-08-28", "actual_balance":"10129.43", "available_balance":"10129.43" } ] } }
07-05 20:21:58.001 8178-8178/? D/TEST: JSON String                  : {"login":{"error":false,"user":{"br_code":12,"mem_id":13,"username":"novalyn","email":"[email protected]","created_at":"2016-07-22 09:05:21"}},"accounts":{"error":false,"sl_summ":[{"sl_desc":"PA : Savings Account","tr_date":"2015-08-17","actual_balance":"483.67","available_balance":"483.67"},{"sl_desc":"PA : Savings - Cash Bond","tr_date":"2015-08-28","actual_balance":"10129.43","available_balance":"10129.43"}]}}
07-05 20:21:58.001 8178-8178/? D/TEST: -error attribute             : false
07-05 20:21:58.001 8178-8178/? D/TEST: User                         : {"br_code":12,"mem_id":13,"username":"novalyn","email":"[email protected]","created_at":"2016-07-22 09:05:21"}
07-05 20:21:58.001 8178-8178/? D/TEST: -br_code attribute           : 12
07-05 20:21:58.001 8178-8178/? D/TEST: -mem_id attribute            : 13
07-05 20:21:58.001 8178-8178/? D/TEST: -username attribute          : novalyn
07-05 20:21:58.001 8178-8178/? D/TEST: -email attribute             : [email protected]
07-05 20:21:58.001 8178-8178/? D/TEST: -created_at attribute        : 2016-07-22 09:05:21
07-05 20:21:58.001 8178-8178/? D/TEST: -error attribute             : false
07-05 20:21:58.001 8178-8178/? D/TEST: -sl_summ array               : [{"sl_desc":"PA : Savings Account","tr_date":"2015-08-17","actual_balance":"483.67","available_balance":"483.67"},{"sl_desc":"PA : Savings - Cash Bond","tr_date":"2015-08-28","actual_balance":"10129.43","available_balance":"10129.43"}]
07-05 20:21:58.002 8178-8178/? D/TEST: -sl_desc attribute           : PA : Savings Account
07-05 20:21:58.002 8178-8178/? D/TEST: -tr_date attribute           : 2015-08-17
07-05 20:21:58.002 8178-8178/? D/TEST: -actual_balance attribute    : 483.67
07-05 20:21:58.002 8178-8178/? D/TEST: -available_balance attribute : 483.67
07-05 20:21:58.002 8178-8178/? D/TEST: ---------------------------------
07-05 20:21:58.002 8178-8178/? D/TEST: -sl_desc attribute           : PA : Savings - Cash Bond
07-05 20:21:58.002 8178-8178/? D/TEST: -tr_date attribute           : 2015-08-28
07-05 20:21:58.002 8178-8178/? D/TEST: -actual_balance attribute    : 10129.43
07-05 20:21:58.002 8178-8178/? D/TEST: -available_balance attribute : 10129.43
07-05 20:21:58.002 8178-8178/? D/TEST: ---------------------------------


07-05 20:21:58.002 8178-8178/? D/TEST: 2 OBJECTS IN ARRAY             
07-05 20:21:58.002 8178-8178/? D/TEST: INPUT String                   { "input": [ { "error":false, "user":{ "br_code":12, "mem_id":13, "username":"novalyn", "email":"[email protected]", "created_at":"2016-07-22 09:05:21" } }, { "error":false, "sl_summ":[ { "sl_desc":"PA : Savings Account", "tr_date":"2015-08-17", "actual_balance":"483.67", "available_balance":"483.67" }, { "sl_desc":"PA : Savings - Cash Bond", "tr_date":"2015-08-28", "actual_balance":"10129.43", "available_balance":"10129.43" } ] } ] }
07-05 20:21:58.002 8178-8178/? D/TEST: JSON String                  : {"input":[{"error":false,"user":{"br_code":12,"mem_id":13,"username":"novalyn","email":"[email protected]","created_at":"2016-07-22 09:05:21"}},{"error":false,"sl_summ":[{"sl_desc":"PA : Savings Account","tr_date":"2015-08-17","actual_balance":"483.67","available_balance":"483.67"},{"sl_desc":"PA : Savings - Cash Bond","tr_date":"2015-08-28","actual_balance":"10129.43","available_balance":"10129.43"}]}]}
07-05 20:21:58.002 8178-8178/? D/TEST: -error attribute             : false
07-05 20:21:58.002 8178-8178/? D/TEST: User                         : {"br_code":12,"mem_id":13,"username":"novalyn","email":"[email protected]","created_at":"2016-07-22 09:05:21"}
07-05 20:21:58.002 8178-8178/? D/TEST: -br_code attribute           : 12
07-05 20:21:58.002 8178-8178/? D/TEST: -mem_id attribute            : 13
07-05 20:21:58.002 8178-8178/? D/TEST: -username attribute          : novalyn
07-05 20:21:58.002 8178-8178/? D/TEST: -email attribute             : [email protected]
07-05 20:21:58.002 8178-8178/? D/TEST: -created_at attribute        : 2016-07-22 09:05:21
07-05 20:21:58.002 8178-8178/? D/TEST: -error attribute             : false
07-05 20:21:58.002 8178-8178/? D/TEST: -sl_summ array               : [{"sl_desc":"PA : Savings Account","tr_date":"2015-08-17","actual_balance":"483.67","available_balance":"483.67"},{"sl_desc":"PA : Savings - Cash Bond","tr_date":"2015-08-28","actual_balance":"10129.43","available_balance":"10129.43"}]
07-05 20:21:58.002 8178-8178/? D/TEST: -sl_desc attribute           : PA : Savings Account
07-05 20:21:58.002 8178-8178/? D/TEST: -tr_date attribute           : 2015-08-17
07-05 20:21:58.002 8178-8178/? D/TEST: -actual_balance attribute    : 483.67
07-05 20:21:58.002 8178-8178/? D/TEST: -available_balance attribute : 483.67
07-05 20:21:58.002 8178-8178/? D/TEST: ---------------------------------
07-05 20:21:58.002 8178-8178/? D/TEST: -sl_desc attribute           : PA : Savings - Cash Bond
07-05 20:21:58.002 8178-8178/? D/TEST: -tr_date attribute           : 2015-08-28
07-05 20:21:58.002 8178-8178/? D/TEST: -actual_balance attribute    : 10129.43
07-05 20:21:58.002 8178-8178/? D/TEST: -available_balance attribute : 10129.43
07-05 20:21:58.002 8178-8178/? D/TEST: ---------------------------------

मेरे पास उन सभी आंतरिक PHP फ़ाइलों तक पहुंच नहीं है जिनका उपयोग मैं आपके PHP कोड को चलाने के लिए कर सकता हूं, इसलिए मैंने नमूना प्रतिक्रिया पेलोड में साझा किए गए हार्ड कोड वाले मानों के साथ सभी फ़ंक्शन कॉलों को बदल दिया। यहाँ OPTION1 प्रारूप में JSON ऑब्जेक्ट उत्पन्न करने के लिए कोड है।

संक्षेप में, आपको $response में सभी उप विशेषताओं के आगे ["लॉगिन"] और ["खाते"] जोड़ना होगा ताकि उन्हें सही JSON ऑब्जेक्ट में समूहीकृत किया जा सके और आपके पास दो JSON ऑब्जेक्ट होंगे जिन्हें ऊपर से पार्स किया जा सकता है एंड्रॉइड साझा कोड।

<?php
// json response array
$br_response = array("error" => FALSE);
$sl_response["error"] = FALSE;
$sl_response["sl_summ"] = array();

$arclass = "13";
$loanclass = "12";
$accintreceivable = "21";

        // user is found
        $response["login"]["error"] = FALSE;
        $response["login"]["user"]["br_code"] = 12;
        $response["login"]["user"]["mem_id"] = 13;
        $response["login"]["user"]["username"] = "novalyn";
        $response["login"]["user"]["email"] = "[email protected]";
        $response["login"]["user"]["created_at"] = "2016-07-22 09:05:21";
                 for($i = 0; $i < 2; $i++){
                        $item = array();
                        $item["sl_desc"] = "PA : Savings Account";
                        $item["tr_date"] = "2015-08-17";
                        $item["actual_balance"] = "483.67";
                        $item["available_balance"] = "483.67";
                        $sl_response["sl_summ"][] = $item;
                    }
                    $response["accounts"] = $sl_response;
                    json_encode($response);
                    echo json_encode($response, true);

PHP नमूना रन ने JSON प्रतिक्रिया उत्पन्न की (OPTION1)

{
   "login":{
      "error":false,
      "user":{
         "br_code":12,
         "mem_id":13,
         "username":"novalyn",
         "email":"[email protected]",
         "created_at":"2016-07-22 09:05:21"
      }
   },
   "accounts":{
      "error":false,
      "sl_summ":[
         {
            "sl_desc":"PA : Savings Account",
            "tr_date":"2015-08-17",
            "actual_balance":"483.67",
            "available_balance":"483.67"
         },
         {
            "sl_desc":"PA : Savings Account",
            "tr_date":"2015-08-17",
            "actual_balance":"483.67",
            "available_balance":"483.67"
         }
      ]
   }
}

कोड कुछ दिनों के लिए https://codepad.remoteinterview.io/YJJKVUEAAAH

पर उपलब्ध रहेगा।

  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. क्या हर बार एप्लिकेशन शुरू होने पर SQLite टेबल बनाना वाकई जरूरी है?

  2. Node.js और SQLite के साथ एक ऑफ़लाइन-प्रथम एप्लिकेशन बनाना

  3. SQLite में 4 सारणीबद्ध आउटपुट मोड

  4. SQLite में JSON फ़ंक्शन और ऑपरेटर (पूरी सूची)

  5. Android 2.X और 3.X पर SQLiteOpenHelper के साथ समस्या