-1

我从 web api 获取一个字符串,例如 {"time":"08:05","code":"CSTM","name":"MUMBAI CST"} ,这个 String 不能转换为 JSONObject ,因此我我无法从字符串中获取值。如何从字符串中获取值?

4

1 回答 1

0
           link = new URL("The url");
           InputStream in = link.openStream();
          LoadRegistration_Api(in);

            protected void LoadRegistration_Api(InputStream json)   
                                     throws IOException, JSONException {
        // TODO Auto-generated method stub

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                json));
        StringBuilder sb = new StringBuilder();
        String line = reader.readLine();
        while (line != null) {
            sb.append(line);
            line = reader.readLine();
        }
        reader.close();

        JSONObject jobj = new JSONObject(sb.toString());

        if (jobj.has("time")) // handle Se_wesam

        {
            Result = jobj.getString("time");

        }

        if (jobj.has("code")) // handle Se_wesam

        {
            UserId = jobj.getString("code");

        }

    }
}

试试这个,我认为它会有所帮助

于 2013-02-08T09:28:30.103 回答