Java 如何在获取整个JSON数据源的同时获取特定标记
我想在JSON文件中记录一些标记的值。 这是我的数据源: 我使用这里的代码成功地获取了整个数据流:我发布了它,以便您更容易检查我的代码:Java 如何在获取整个JSON数据源的同时获取特定标记,java,android,json,Java,Android,Json,我想在JSON文件中记录一些标记的值。 这是我的数据源: 我使用这里的代码成功地获取了整个数据流:我发布了它,以便您更容易检查我的代码: Button btnHit; TextView txtJson; ProgressDialog pd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.
Button btnHit;
TextView txtJson;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnHit = (Button) findViewById(R.id.btnHit);
txtJson = (TextView) findViewById(R.id.tvJsonItem);
btnHit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new JsonTask().execute("http://data.nba.net/10s/prod/v1/2016/players.json");
}
});
}
private class JsonTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Please wait");
pd.setCancelable(false);
pd.show();
}
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd.isShowing()){
pd.dismiss();
}
txtJson.setText(result);
}
}
}
按钮btnHit;
TextView-txtJson;
进展性帕金森病;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnHit=(按钮)findViewById(R.id.btnHit);
txtJson=(TextView)findViewById(R.id.tvJsonItem);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建JsonTask()。执行(“http://data.nba.net/10s/prod/v1/2016/players.json");
}
});
}
私有类JsonTask扩展了异步任务{
受保护的void onPreExecute(){
super.onPreExecute();
pd=新建进度对话框(MainActivity.this);
pd.setMessage(“请稍候”);
pd.可设置可取消(假);
pd.show();
}
受保护的字符串doInBackground(字符串…参数){
HttpURLConnection=null;
BufferedReader reader=null;
试一试{
URL=新URL(参数[0]);
connection=(HttpURLConnection)url.openConnection();
connection.connect();
InputStream=connection.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(流));
StringBuffer=新的StringBuffer();
字符串行=”;
而((line=reader.readLine())!=null){
buffer.append(第+行“\n”);
Log.d(“Response:”,“>”+行);//在这里您将得到完整的响应
}
返回buffer.toString();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
if(连接!=null){
连接断开();
}
试一试{
if(读卡器!=null){
reader.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
if(pd.isShowing()){
pd.解散();
}
setText(结果);
}
}
}
我的问题是,我无法从我选择的源中获得一个标记。例如,我如何获得该流中每个玩家的名字和姓氏并记录它
感谢您的时间和考虑。首先,我建议您使用android的任何http库(okHttp,volley…) 但是,如果您仍然希望使用实现它的方式,则需要在此处进行一些更改:
while ((line = reader.readLine()) != null) {
buffer.append(line);
Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
}
String json = buffer.toString();
try {
String json = "";
JSONObject jsonObject = new JSONObject(json);
JSONObject league = jsonObject.getJSONObject("league");
JSONArray standard = league.getJSONArray("standard");
for (int i = 0;i<standard.length();i++){
JSONObject item = standard.getJSONObject(i);
String name = item.getString("firstName");
String lastName= item.getString("lastName");
}
} catch (JSONException e) {
e.printStackTrace();
}
while((line=reader.readLine())!=null){
buffer.append(行);
Log.d(“Response:”,“>”+行);//在这里您将得到完整的响应
}
字符串json=buffer.toString();
试一试{
字符串json=“”;
JSONObject JSONObject=新的JSONObject(json);
JSONObject-league=JSONObject.getJSONObject(“联盟”);
JSONArray标准=league.getJSONArray(“标准”);
对于(int i=0;i首先将整个对象转换为一个JSON对象。SonoObject mainObject=new JSONObject(您的数据);然后您可以获得league对象:JSONObject league=mainObject.getJsonObject(“league”);从那里您可以获得玩家数组,类似leage.getJSONArray(“标准”)。我还没有测试过这个,它可能有输入错误,因为我只是快速地从脑海中输入。但这是一个很好的起点。请您发送一些工作代码给我好吗?我没有使用这个的经验,所以最好先有工作代码。感谢您尝试使用改装或okHttp库来降低复杂性。或者从以下位置下载代码:What对象的类型是buffer?这与您已经使用的对象相同StringBuffer buffer=new StringBuffer();