[英]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. 我将Drupal Services和JSON Services模块一起用作数据源。
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. 我正在使用DrupalCloud库https://github.com/skyred/DrupalCloud/wiki ,并且想知道如何最好地处理从userLogin()调用中收到的结果。
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 像这篇文章一样,来自2小时前: 如何将json对象转换为类对象,或者: Android中的JSON解析
Or just search for yourself: Search: Android+Json Should give you a good idea.. 或只是搜索自己: 搜索:Android + Json应该给你一个好主意。
This is one way to parse the last JSON message in your question: 这是解析问题中最后一条JSON消息的一种方法:
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...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.