简体   繁体   中英

How do I deserialize Drupal JSON Services strings in Android?

I am using Drupal Services along with the JSON Services module as a data source.

I am using the DrupalCloud library, https://github.com/skyred/DrupalCloud/wiki , and am wondering how to best process the results that I receive from a userLogin() call.

If the call itself fails we get:

{
  "#error": true,
  "#message": "Some message"
}

If the call succeeds but the login credentials are wrong:

{
  "#error": false,
  "#data": {
    "#error": true,
    "#message": "Some message"
  }
}

If the call success and the login credentials are correct, it returns:

{
  "#error": false,
  "#data": {
    "sessid": "foo",
    "user": {
      "uid": "69",
      "name": "Russell Jones",
      "pass": "bar",
      "mail": "russell@test.net",
      "roles": {
        "2": "authenticated user",
        "5": "Student"
      },
    }
  }
}

How do I go about using this data meaningfully? Or rather, how do I test to see if the call worked, and if the login was successful or not.

Have you searched older posts? Like this post, from 2 hours ago: how to convert json object into class object or: JSON Parsing in Android

Or just search for yourself: Search: Android+Json Should give you a good idea..

This is one way to parse the last JSON message in your question:

public void readJsonString(String jsonString) throws JSONException
{
    JSONObject jsonObject = new JSONObject(jsonString);
    boolean error = jsonObject.getBoolean("#error");
    if (!error)
    {
        JSONObject data = jsonObject.getJSONObject("#data");
        String sessionId = data.getString("sessid");
        JSONObject user = data.getJSONObject("user");
        int uid = user.getInt("uid");
        String name = user.getString("name");

        // you get the pattern, same way with the other fields...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM