JSONObject有值,但返回空值

时间:2019-02-22 05:39:26

标签: java android json api

[
{
    "valve": "4679",
    "selling": "5516",
    "bal": "9075.4",
    "o id": "37",
    "invested": "11122",      //<<<<<<<- this value returns null
    "aProfit": "1012",      //<<<<<<<- this value returns null
    "count": "182",      //<<<<<<<- this value returns null
    "cost": "5051"      //<<<<<<<- this value returns null
}
]

。-从onPostExecute请求的上述JSONObject

    @Override
protected void onPostExecute (String ANSWER)
{   String u_id;

    try{
        JSONArray jsonArray = null;
        jsonArray = new JSONArray(ANSWER);
        for (int i = 0; i < jsonArray.length(); )
        {
            JSONObject JO = (JSONObject) jsonArray.get(i);
            jsonObject = jsonArray.getJSONObject(i);

            CASH = (String) jsonObject.getString("bal");
            USER_VALUE = (String) jsonObject.getString("valve");
            INVEST = (String) jsonObject.getString("invested");
            PROFIT = (String) jsonObject.getString("aProfit");
            COST_P = (String) jsonObject.getString("cost");
            COUNT = (String) jsonObject.getString("count");

    DashBoard.mprofit.setText(PROFIT);
    DashBoard.minvest.setText(INVEST);
    DashBoard.massets.setText(COST_P);
    DashBoard.mvalue.setText(USER_VALUE);  //<<<<<<<- this value returns the value.

许多其他人也这样做,但是有些人拒绝退货,当我与邮递员进行交叉核对时,他们全都回来了。 所以现在很困惑,因为如果我在网络服务代码中交换valvecount的位置,它将不再为null,反之亦然。

简短的问题:有人可以解释一下为什么某些值在java编码中返回null。

3 个答案:

答案 0 :(得分:0)

 try {

            JSONArray jsonArray = null;
            jsonArray = new JSONArray(ANSWER);
            for (int i = 0; i < jsonArray.length(); ) {
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                jsonObject = jsonArray.getJSONObject(i);

                String CASH = (String) jsonObject.getString("bal");
                String USER_VALUE = (String) jsonObject.getString("valve");
                String INVEST = (String) jsonObject.getString("invested");
                String PROFIT = (String) jsonObject.getString("aProfit");
                String COST_P = (String) jsonObject.getString("cost");
                String COUNT = (String) jsonObject.getString("count");


            }
        }catch (Exception e){
        }

答案 1 :(得分:0)

尝试{

        JSONArray jsonArray = null;
        jsonArray = new JSONArray(ANSWER);
        for (int i = 0; i < jsonArray.length(); ) {
            JSONObject jsonObject = (JSONObject) jsonArray.get(i);
            //jsonObject = jsonArray.getJSONObject(i); **remove this line thne check remove this line thne check**

            String CASH = (String) jsonObject.getString("bal");
            String USER_VALUE = (String) jsonObject.getString("valve");
            String INVEST = (String) jsonObject.getString("invested");
            String PROFIT = (String) jsonObject.getString("aProfit");
            String COST_P = (String) jsonObject.getString("cost");
            String COUNT = (String) jsonObject.getString("count");


        }
    }catch (Exception e){
    }

答案 2 :(得分:0)

尝试使用此代码:

try {

        JSONArray jsonArray = null;
        jsonArray = new JSONArray(ANSWER);
        for (int i = 0; i < jsonArray.length(); ) {
            JSONObject jsonObject = (JSONObject) jsonArray.get(i);
            jsonObject = jsonArray.getJSONObject(i);

            String valve = (String) jsonObject.getString("valve");
            String selling = (String) jsonObject.getString("selling");
            String bal = (String) jsonObject.getString("bal");
            String o_id = (String) jsonObject.getString("o_id");
            String invested = (String) jsonObject.getString("invested");
            String aProfit = (String) jsonObject.getString("aProfit");
            String count = (String) jsonObject.getString("count");
            String cost = (String) jsonObject.getString("cost");
        }
    }catch (Exception e){
    }

希望这会有所帮助... 快乐编码:)