json解析android
Here you will get android json parsing from url example.
在这里,您将从url示例获取android json解析。
What I will do here?
我将在这里做什么?
The data contains a json array with some fruits name as shown below.
数据包含一个带有一些水果名称的json数组,如下所示。
{
"fruits":[
"Apple",
"Apricot",
"Avocado",
"Banana",
"Bilberry",
"Blackberry",
"Blackcurrant",
"Blueberry",
"Boysenberry",
"Currant",
"Cherry",
"Cherimoya",
"Cloudberry",
"Coconut",
"Cranberry",
"Custard apple",
"Damson",
"Date",
"Dragonfruit",
"Durian",
"Elderberry",
"Feijoa",
"Fig",
"Goji berry",
"Gooseberry",
"Grape",
"Raisin",
"Grapefruit",
"Guava"
]
}
Also Read: Picasso Android Tutorial – Load Image from URL
另请阅读: Picasso Android教程–从URL加载图像
Create a new android studio project with package name com.jsonparsing.
使用包名称com.jsonparsing创建一个新的android studio项目。
As we are fetching data from internet so we have to add internet access permission. Add following line of code in AndroidManifest.xml file.
由于我们正在从Internet上获取数据,因此我们必须添加Internet访问权限。 在AndroidManifest.xml文件中添加以下代码行。
I am using volley library in this example. It is a network library that is used to fetch data from server or internet. So add it to your project by adding following line of code in app level build.gradle file under dependencies section. After that sync the project.
我在这个例子中使用排球库 。 它是一个网络库,用于从服务器或Internet提取数据。 因此,通过在依赖项部分的应用程序级别build.gradle文件中添加以下代码行,将其添加到您的项目中。 之后,同步项目。
compile 'com.android.volley:volley:1.0.0'
The following code is responsible for fetching json data from url in the form of json string using volley library.
以下代码负责使用凌空库以json字符串的形式从url中获取json数据。
StringRequest request = new StringRequest(url, new Response.Listener() {
@Override
public void onResponse(String string) {
parseJsonData(string);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getApplicationContext(), "Some error occurred!!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
When data is fetched successfully then onResponse() method is called.
成功获取数据后,将调用onResponse()方法。
If there is any error while fetching data then onErrorResponse() is called and an error message is displayed in toast.
如果在获取数据时出现任何错误,则将调用onErrorResponse()并在烤面包中显示一条错误消息。
After creating the volley request we have to add it to request queue.
创建齐发请求后,我们必须将其添加到请求队列。
RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
rQueue.add(request);
Now comes the parsing part. The parseJsonData() method contains the code to parse the json string.
现在是解析部分。 parseJsonData()方法包含用于解析json字符串的代码。
void parseJsonData(String jsonString) {
try {
JSONObject object = new JSONObject(jsonString);
JSONArray fruitsArray = object.getJSONArray("fruits");
ArrayList al = new ArrayList();
for(int i = 0; i < fruitsArray.length(); ++i) {
al.add(fruitsArray.getString(i));
}
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, al);
fruitsList.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
dialog.dismiss();
}
First json string is parsed to json object. From this object a json array with name fruits is fetched.
第一个json字符串被解析为json对象。 从该对象获取名称为fruits的json数组。
Below I have shared the full source code.
下面,我分享了完整的源代码。
activity_main.xml
activity_main.xml
MainActivity.java
MainActivity.java
package com.jsonparsing;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView fruitsList;
String url = "https://www.thecrazyprogrammer.com/example_data/fruits_array.json";
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fruitsList = (ListView)findViewById(R.id.fruitsList);
dialog = new ProgressDialog(this);
dialog.setMessage("Loading....");
dialog.show();
StringRequest request = new StringRequest(url, new Response.Listener() {
@Override
public void onResponse(String string) {
parseJsonData(string);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getApplicationContext(), "Some error occurred!!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
rQueue.add(request);
}
void parseJsonData(String jsonString) {
try {
JSONObject object = new JSONObject(jsonString);
JSONArray fruitsArray = object.getJSONArray("fruits");
ArrayList al = new ArrayList();
for(int i = 0; i < fruitsArray.length(); ++i) {
al.add(fruitsArray.getString(i));
}
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, al);
fruitsList.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
dialog.dismiss();
}
}
Screenshots
屏幕截图
You can ask your queries related to above android json paring from url example.
您可以从url示例询问与上述android json配对相关的查询。
翻译自: https://www.thecrazyprogrammer.com/2017/01/android-json-parsing-from-url-example.html
json解析android