-4

I calling wsdl webservice and get following JSON string

eg :

$result = '
{
"Value":
[
{"Username":"CustomerName1","Password":"123","ResellerID":"888"},{"Username":"CustomerName2","Password":"123orAnyChar","ResellerID":"378"}
],
"Error":{"Check":false,"Msg":"No Error!"}
}
';

How to convert this php code to java code :

Example php code :

$MyArray = json_decode($result, true);

if (array_key_exists("Error", $MyArray)) {

    if ($MyArray['Error']['Check'] != true) {

        foreach ($MyArray['Value'] as $Key => $Val) {
            echo "Username = ".$Val['Username']." , Pass = ".$Val['Password']." , ResID = ".$Val['ResellerID']."\r\n";
        }

    }
    else {
        echo "Error Msg";
    }

}

Note : just convert this php code block to java, using sample json string

thanks

  • See http://stackoverflow.com/questions/1927885/decode-json-data-in-java and http://stackoverflow.com/questions/9256669/java-built-in-data-parser-for-json-or-xml-or-else – gknicker Jan 04 '15 at 21:13

1 Answers1

0

Answer for my question is :

try {

            // result is json string

            Boolean ErrStatus = true;

            JSONObject CheckErr = new JSONObject(result).getJSONObject("Error");
            ErrStatus = CheckErr.getBoolean("Check");

            String Msg = "";
            if (ErrStatus == false) {

                JSONArray jArray = new JSONObject(result).getJSONArray("Value");
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json = jArray.getJSONObject(i);
                    Msg = Msg + "Username : " + json.getString("Username") + "\n" + "Password : " + json.getString("Password") + "\n\n";
                }

                Msg = "Info correct";

            }
            else {
                Msg = CheckErr.getString("Msg");
            }


            txtLogin.setText(s);

        }
        catch (Exception e) {
            Log.i("WS", "Error JSON");
        }