如何保存可变字符串的一部分?

how do I save a part of a variable string?

我收到了回复,但每个用户的回复都不一样。 我想提取用户 ID 和名称。我该怎么做?

响应字符串:

{
    "sessionkey":"f4b54dedlfkjhgdlfkjghdfslkjgh1a255242bf71597f4b35ac882f0702",
    "user" : {
        "userID" : "1",
        "name":"Test"
    }
}

我要保存 1 = (String userid) 并保存 Test = (String name)

提前致谢

这只是一个简单的 JSON blob,因此请使用 JSON 解析器提取数据。

我选择的库是org.json.json

String blob = "{ origin string }";

// create a new json object from the main blob
JSONObject root = new JSONObject(blob);

// extract the session key
String sessionKey = root.getString("sessionkey");

// extract the user object
JSONObject user = root.getJSONObject("user");

// extract the user id
String userId = user.getString("userID");