Java JSONObject 返回类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30368359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSONObject return type
提问by Avinash
The following android application code in Java outputs to:
Java 中的以下 android 应用程序代码输出到:
[{"handle":"DmitriyH","firstName":"Dmitriy","lastName":"Khodyrev","country":"Russia","city":"Moscow","organization":"KL","contribution":122,"rank":"master","rating":2040,"maxRank":"international master","maxRating":2072,"lastOnlineTimeSeconds":1432130513,"registrationTimeSeconds":1268570311}]}
when extracting data from codeforces api- http://codeforces.com/api/user.info?handles=DmitriyH;
从 codeforces api- http://codeforces.com/api/user.info?handles=DmitriyH提取数据时 ;
but I only want "firstName" of the user. Can anyone recommend changes to my code??
但我只想要用户的“名字”。任何人都可以建议更改我的代码吗?
public class Http extends Activity {
TextView httpStuff;
HttpClient client;
JSONObject json;
final static String URL = http://codeforces.com/api/user.info?handles=;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httpex);
httpStuff = (TextView)findViewById(R.id.tvHttp);
client = new DefaultHttpClient();
new Read().execute("result");
}
public JSONObject lastSub(String username) throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(username);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
//httpStuff.setText("xxx");
int status = r.getStatusLine().getStatusCode();
if(status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject last = new JSONObject(data);
return last;
}
else {
Toast.makeText(Http.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask <String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = lastSub("avm12");
return json.getString(arg0[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
httpStuff.setText(result);
}
}
}
}
采纳答案by Marco Dufal
public JSONObject lastSub(String username) throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(username);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
//httpStuff.setText("xxx");
int status = r.getStatusLine().getStatusCode();
if(status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject last = new JSONObject(data).getJSONArray("result").getJSONObject(0);
return last;
}
else {
Toast.makeText(Http.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
EDIT
编辑
Now suppose I am using the url "codeforces.com/api/user.status?handle=avm12"; and I want to extract the first ten( or say n number of them) "problem" tags. What should I do then?
现在假设我正在使用 url "codeforces.com/api/user.status?handle=avm12"; 我想提取前十个(或说其中的 n 个)“问题”标签。那我该怎么办?
Well first get the root JSONArray
那么首先得到根 JSONArray
JSONArray array= new JSONObject(data).getJSONArray("result");
Then in a for loop get all the needed JSONObjects
然后在 for 循环中获取所有需要的 JSONObjects
for(int k=0;k<n;k++){ // n must be less than array.length()
JSONObject problemObject=array.getJSONObject(k).getJSONObject("problem");
//do what you need to do with the problemObject E.G. Add them to an ArryList...
}
Consider adding &count=N
to your url to limit the output to the needed N results...
考虑添加&count=N
到您的 url 以将输出限制为所需的 N 个结果...
回答by CAS
Your complete JSONObject
contains an array with the tag "result"
, so you should extract the firstName like this:
您的 completeJSONObject
包含一个带有 tag 的数组"result"
,因此您应该像这样提取 firstName :
JSONArray result = json.getJSONArray("result");
JSONObject jo = result.getJSONObject(0);
String firstName = jo.getString("firstName");
回答by Piyush
Your first tag is a JSONArray
so you need to make a method with return JSONArray
instead of JSONObject
. As your requirement you can get it through
你的第一个标签是 aJSONArray
所以你需要用 returnJSONArray
而不是JSONObject
. 根据您的要求,您可以通过
jArray.getJSONObject(0).get("firstName");
回答by Flaxie
In the code you posted your not using the correct URL. The following works:
在您发布的代码中,您没有使用正确的 URL。以下工作:
final static String URL = http://codeforces.com/api/user.info?handles=;
...
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = lastSub("DmitriyH");
final JSONArray result = json.getJSONArray("result");
final JSONObject jsonObject = result.getJSONObject(0);
return jsonObject.getString("firstName");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
....