Android Studio中从URL解析Json文件数据
我对这个Json文件有问题。我试图做一个解析器,在按下按钮后在文本视图中显示结果。我不会解析“firstName”标签,但要做到这一点,我需要通过其他标签,如“league”和“standard”。我不知道如何在这个Json文件中解析它们。 Json文件的结构如下:Android Studio中从URL解析Json文件数据,android,Android,我对这个Json文件有问题。我试图做一个解析器,在按下按钮后在文本视图中显示结果。我不会解析“firstName”标签,但要做到这一点,我需要通过其他标签,如“league”和“standard”。我不知道如何在这个Json文件中解析它们。 Json文件的结构如下: { "_internal":{ "pubDateTime":"2017-06-23 03:21:52.076", "xslt":"xsl/league/roster/marty_active_players.xsl"
{
"_internal":{
"pubDateTime":"2017-06-23 03:21:52.076",
"xslt":"xsl/league/roster/marty_active_players.xsl",
"eventName":"league_roster"
},
"league":{
"standard":[
{
"firstName":"Alex",
"lastName":"Abrines",
"personId":"203518",
"teamId":"1610612760 1610612760",
"jersey":"8",
"pos":"G-F",
"heightFeet":"6",
"heightInches":"6",
"heightMeters":"1.98",
"weightPounds":"190",
"weightKilograms":"86.2",
"dateOfBirthUTC":"1993-08-01",
"teams":[
{
"teamId":"1610612760",
"seasonStart":"2016",
"seasonEnd":"2016"
}
],
"draft":{
"teamId":"1610612760",
"pickNum":"32",
"roundNum":"2",
"seasonYear":"2013"
},
"nbaDebutYear":"2016",
"yearsPro":"0",
"collegeName":"",
"lastAffiliation":"Spain/Spain",
"country":"Spain"
},
这是我的Java代码:
public class MainActivity extends AppCompatActivity {
Button btnHit;
TextView txtJson;
ProgressDialog pd;
String Players[] = new String[100];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnHit = (Button) findViewById(R.id.btn);
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);
try {
JSONObject jsonObject = new JSONObject(result);
String DataPlayers = jsonObject.getString("standard");
JSONArray jsonArray = new JSONArray(DataPlayers);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject JO = jsonArray.getJSONObject(i);
Players[i] = JO.getString("firstName");
txtJson.append(Players[i] + "\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
if (pd.isShowing()) {
pd.dismiss();
}
}
}
public类MainActivity扩展了AppCompatActivity{
按钮btnHit;
TextView-txtJson;
进展性帕金森病;
弦乐演奏者[]=新弦乐[100];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnHit=(按钮)findViewById(R.id.btn);
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(结果);
试一试{
JSONObject JSONObject=新JSONObject(结果);
字符串DataPlayers=jsonObject.getString(“标准”);
JSONArray JSONArray=新的JSONArray(数据播放器);
for(int i=0;i
您的代码应该是这样的
try {
String result = "your_json_string";
JSONObject jsonObject = new JSONObject(result);
JSONObject leagueObject = jsonObject.getJSONObject("league");
JSONArray standardArray = leagueObject.getJSONArray("standard");
for (int i = 0; i < standardArray.length(); i++) {
JSONObject standardObject = standardArray.getJSONObject(i);
String name = standardObject.getString("firstName");
}
} catch (Exception e) {
System.out.println(e);
}
试试看{
String result=“your_json_String”;
JSONObject JSONObject=新JSONObject(结果);
JSONObject leagueObject=JSONObject.getJSONObject(“联盟”);
JSONArray standardArray=leagueObject.getJSONArray(“标准”);
对于(int i=0;i
错误:
JSONObject jsonObject = new JSONObject(result);
String DataPlayers = jsonObject.getString("standard");
JSONObject jsonObject = new JSONObject(result);
JsonObject objStandard = jsonObject.getJSONObject("league");
正确:
JSONObject jsonObject = new JSONObject(result);
String DataPlayers = jsonObject.getString("standard");
JSONObject jsonObject = new JSONObject(result);
JsonObject objStandard = jsonObject.getJSONObject("league");
现在,您可以从objStandard对象检索json数组并遍历子json对象
JSONArray jsonArray = objStandard.getJSONArray("standard");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject JO = jsonArray.getJSONObject(i);
Players[i] = JO.getString("firstName");
txtJson.append(Players[i] + "\n");
}
JSONArray-JSONArray=objStandard.getJSONArray(“标准”);
for(int i=0;i
2美分建议:)
您似乎刚刚开始探索android开发,说基于您使用的是传统的json解析方法,我建议您查看库,这是一个Java序列化/反序列化库,用于将Java对象转换为json并返回。简言之,您不需要手动解析json响应,而是您自己将有正常的java类对象和成员/方法要处理。可能重复的