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 stackoverflow.com/questions/1927885/decode-json-data-in-java and stackoverflow.com/questions/9256669/…gknicker– gknicker2015年01月04日 21:13:51 +00:00Commented Jan 4, 2015 at 21:13
1 Answer 1
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");
}
Sign up to request clarification or add additional context in comments.