【问题标题】:How to parse a JSON string in android如何在android中解析JSON字符串
【发布时间】:2014-06-14 05:05:29
【问题描述】:

我的 Web 服务输出一个我需要解析的 JSON 字符串。

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

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

【问题讨论】:

    标签: android json parsing


    【解决方案1】:
      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
    

    【讨论】:

      【解决方案2】:
      String jsonString = ""; //This'll contain the jsonString you mentioned above. 
      
      JSONObject object = new JSONObject(jsonString);
      String accessToken = object.getString("access_token");
      

      类似地获取其他值。

      【讨论】:

        【解决方案3】:
        {  // 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);
        

        【讨论】:

          【解决方案4】:
          JSONObject jsonObject = new JSONObject(jsonString);
          

          【讨论】:

          • @jekiran 你应该接受对你帮助最大的答案
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-10-02
          • 1970-01-01
          • 2011-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多