如何解析android中的JSON字符串

时间:2014-06-14 05:05:29

标签: android json parsing

我的网络服务输出了我需要解析的JSON字符串。

您能否告诉我如何解析给定的JSON字符串:

{
"access_token":"kfwsfdcscfnsdcfsdsdfsd6f7df9sd6f89sd6s",
 "expires_in":3600,
 "token_type":"bearer",
 "scope":null,
 "refresh_token":"5dfddf1d6dfd1fddgdgdg1dg56d1"
 }

4 个答案:

答案 0 :(得分:2)

  JSONObject mainObject = new JSONObject(Your_Sring_data);
  JSONObject uniObject = mainObject.getJSONObject("university");
  String  uniName = uniObject.getJSONObject("name");
  String uniURL = uniObject.getJSONObject("url");

  JSONObject oneObject = mainObject.getJSONObject("1");
  String id = oneObject.getJSONObject("id");

请参阅此链接:

http://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android

答案 1 :(得分:2)

String jsonString = ""; //This'll contain the jsonString you mentioned above. 

JSONObject object = new JSONObject(jsonString);
String accessToken = object.getString("access_token");

同样获取其他值。

答案 2 :(得分:1)

JSONObject jsonObject = new JSONObject(jsonString);

答案 3 :(得分:1)

{  // represents json object node
"access_token":"kfwsfdcscfnsdcfsdsdfsd6f7df9sd6f89sd6s", // key value pair

解析

 JSONObject jb = new JSONObject("jsonstring");
 String acess_token = jb.getString("acess_token"); // acess_token is the key
 // similarly others

您还可以使用Gson将json字符串转换为java对象。

http://code.google.com/p/google-gson/

使用Gson

将jar添加到项目libs文件夹

然后

public class Response {

    String acess_token,expires_in,token_typerefresh_token;
}

然后

InputStreamReader isr = new InputStreamReader (is);
Gson gson = new Gson();         
Response lis = new Gson().fromJson(isr, Response.class);
Log.i("Acess Toekn is ",""+lis.expires_in);