开发者

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": "authen开发者_如何转开发ticated 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...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜