【问题标题】:How to convert this php code (json array) to java code on android如何将此php代码(json数组)转换为android上的java代码
【发布时间】:2015-03-02 10:49:42
【问题描述】:

我调用 wsdl webservice 并得到以下 JSON 字符串

例如:

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

如何将此php代码转换为java代码:

示例 php 代码:

$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";
    }

}

注意:只需将此 php 代码块转换为 java,使用示例 json 字符串

谢谢

【问题讨论】:

标签: multidimensional-array


【解决方案1】:

我的问题的答案是:

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");
        }

【讨论】:

    猜你喜欢
    • 2019-11-19
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多