-3

我从 php 主机收到 json 响应

{"uid":"0"}

我想从中检索 0(即 uid 值)

我尝试检索它..但有异常

这是我的代码..

    HttpEntity entity = response.getEntity();

} catch(Exception e) {
    System.out.println("Exception : " + e.getMessage());
}
finally {
    if (conn != null) {
        try {  
            conn.close ();
        }
        catch (Exception e) { e.printStackTrace(); }  
    }
}

//convert response to string
/*
try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line+"\n");
    }
    is.close();
    result = sb.toString();
} catch(Exception e) {
    System.out.println("Exception : " + e.getMessage());
}
try
{
    ??????????????????????????
} catch(Exception e) { e.printStackTrace(); }
4

2 回答 2

2

只需使用JSONOBJECT

JSONObject obj = new JSONObject(src);
if (obj.has("uid")){
   String uid = obj.getString("uid");
}
于 2012-10-30T18:26:29.507 回答
0

使用GSON让这变得非常简单

于 2012-10-30T18:54:23.803 回答