我无法通过使用从保存的首选项类中提取JSONObject

时间:2017-02-09 10:24:09

标签: android json

我无法使用TestCrowd.java类从保存的首选项类中提取JSONObject。

我的JSON

 {
  "success": true,
  "message": {
    "user": {
      "firstName": "aaa",
      "lastName": "aaa",
      "email": "xxx@gmail.com",
      "role": 1,
      "profileUrl": ""
    },
    "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7ImZpcnN0TmFtZSI6ImFhYSIsImxhc3ROYW1lIjoiYWFhIiwiZW1haWwiOiJ4eHhAZ21haWwuY29tIiwicm9sZSI6MSwicHJvZmlsZVVybCI6IiJ9LCJpYXQiOjE0ODY2MzU3NjUsImV4cCI6MTQ4NjY0NzIwNX0.rRlqKNMoBs_AaDKOUlToT5-D5QKb20IjYMuUBqK9G7c",
    "enum": {
      "_id": "5889d3f10893a7a42243998e",
      "updatedAt": "2017-01-26T10:48:17.233Z",
      "createdAt": "2017-01-26T10:48:17.233Z",
      "__v": 0,
      "mailCategories": [
        {
          "color": [
            "Work",
            "Document",
            "Social",
            "Advertising",
            "Client"
          ],
          "value": [
            "#1ab394",
            "#EF5352",
            "#1c84c6",
            "#23c6c8",
            "#F8AC59"
          ]
        }
      ],
      "folders": [
        "Send",
        "Draft"
      ],
      "uploadsImageTypes": [
        "jpg",
        "jpeg",
        "png",
        "gif"
      ],
      "uploadsFileTypes": [
        "jpg",
        "jpeg",
        "docx",
        "pdf",
        "txt",
        "ppt",
        "png",
        "gif"
      ],
      "workOrderStatus": {
        "color": [
          "#A09580",
          "#f8ac59",
          "#41b0f6",
          "#0d71b0",
          "#1ab394",
          "#067e47",
          "#2819bc",
          "#0d08f3",
          "#ef0c34",
          "#ef9aa9",
          "#f2830b"
        ],
        "value": [
          "Draft",
          "Requested",
          "Requested Awaiting Approval",
          "Assigned",
          "Work In Progress",
          "Ready",
          "Done",
          "Closed,Completed",
          "Closed,Incompleted",
          "On Hold",
          "Open"
        ]
      },
      "priorities": {
        "color": [
          "#0B7409",
          "#CE9B9B",
          "#7CC396",
          "#CEBE99"
        ],
        "value": [
          "Hard Down",
          "High",
          "Medium",
          "Low"
        ]
      },
      "categories": [
        "Equipment",
        "Cranes",
        "Delivery Vehicles",
        "Rotating Spares"
      ],
      "projects": [
        "Vehicle New",
        "System #7865",
        "Book Shop",
        "Site"
      ],
      "maintenanceTypes": {
        "color": "#FFFFFF",
        "bg_color": [
          "#1c84c6",
          "#262626",
          "#A09580",
          "#ed5565"
        ],
        "value": [
          "Electrical",
          "Damage",
          "Safty",
          "Broken"
        ]
      },
      "assets": [
        "Conveyor Belt 1",
        "Cranes",
        "Delivery Vehicles",
        "Rotating Spares"
      ],
      "assignToUser": [
        1,
        2,
        4,
        5,
        6
      ],
      "roles": [
        "Admin",
        "Manager",
        "Technician",
        "Customer",
        "Supplier",
        "Engineer",
        "Guest"
      ]
    }
  }
}

TestCrowd.java

public class TestCrowd extends Application {

private JSONObject roles,priorities;

public JSONObject getRoles() {
    return roles;
}

public void setRoles(JSONObject roles) {
    this.roles = roles;
}

public JSONObject getPriorities() {
    return priorities;
}

public void setPriorities(JSONObject priorities) {
    this.priorities = priorities;
}

}

我的主要活动名为NewWorkOrderActivity.java

try {
        JSONObject details = ((TestCrowd) getApplicationContext()).getPriorities();

        JSONObject priority = details.getJSONObject("priorities"); /*Where the null value object reference located*/
        JSONArray priority_arry = priority.getJSONArray("value");
        for (int i = 0; i < priority_arry.length(); i++) {
            arraySpinner_1.add(priority_arry.get(i).toString());

        }
        ArrayAdapter<String> spinnerAdepter_1 = new ArrayAdapter<String>(this,R.layout.spinner_item,arraySpinner_1);
        spinner_1.setAdapter(spinnerAdepter_1);

    } catch (JSONException e) {
        e.printStackTrace();
    }

错误是

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.malith.testcrowd/com.example.malith.testcrowd.NewWorkOrderActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONObject.getJSONObject(java.lang.String)' on a null object reference

我只需要从主Json

中提取优先级 - &gt;值

2 个答案:

答案 0 :(得分:0)

您正在错误地访问json对象,

优先级在消息内 - &gt;枚举 - &gt;优先级

JSONObject obj= ((TestCrowd) getApplicationContext()).getPriorities();

 JSONObject message= obj.getJSONObject("message");

  JSONObject enum= message.getJSONObject("enum");

 JSONObject priorities = enum.getJSONObject("priorities");

 JSONArray value=priorities .getJSONArray("value") 

现在用价值做你想做的事

答案 1 :(得分:0)

使用JSONGSON转换为字符串并将其保存在Preference get as String中并使用GSON转换为类对象

例如检查here