Android应用程序JSON解析
我试图解析从android应用程序中链接的URL接收到的JSON。我想将此数据输出到我的应用程序中的文本视图。下面是源代码 现在,这就是JSON在应用程序上的显示方式Android应用程序JSON解析,android,json,parsing,url,textview,Android,Json,Parsing,Url,Textview,我试图解析从android应用程序中链接的URL接收到的JSON。我想将此数据输出到我的应用程序中的文本视图。下面是源代码 现在,这就是JSON在应用程序上的显示方式 [ { "IndividualID": 1, "FullName": "Sean Kelly", "DOB": "07/06/1987", "MedicalHistory": "Ulcerative Colitis", "Medication": "Asacolon", "Ale
[
{
"IndividualID": 1,
"FullName": "Sean Kelly",
"DOB": "07/06/1987",
"MedicalHistory": "Ulcerative Colitis",
"Medication": "Asacolon",
"Alergies": "Penicillin"
}
]
Fragment2.java(注释掉的部分是我尝试解析的地方,但没有显示任何内容)
包装即药物;
导入java.io.IOException;
导入java.io.InputStream;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.protocol.BasicHttpContext;
导入org.apache.http.protocol.HttpContext;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
公共类Fragment2扩展了Fragment{
TextView-txtId;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(右布局。碎片2_布局,容器,假);
新建LongRunningGetIO().execute();
返回视图;
}
私有类LongRunningGetIO扩展异步任务{
受保护字符串GetAscioContentRomentity(HttpEntity实体)引发IllegalStateException、IOException{
InputStream in=entity.getContent();
StringBuffer out=新的StringBuffer();
int n=1;
而(n>0){
字节[]b=新字节[4096];
n=英寸读数(b);
如果(n>0)out.append(新字符串(b,0,n));
}
return out.toString();
}
@凌驾
受保护字符串doInBackground(无效…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
HttpGet HttpGet=新的HttpGet(“https://api.myjson.com/bins/363s3");
字符串文本=空;
试一试{
HttpResponse response=httpClient.execute(httpGet,localContext);
HttpEntity=response.getEntity();
text=getAsciContentRomentity(实体);
}捕获(例外e){
返回e.getLocalizedMessage();
}
返回文本;
}
受保护的void onPostExecute(字符串结果){
如果(结果!=null){
txtId=(TextView)getView().findViewById(R.id.txtId);
txtId.setText(结果);
/*JSONObject jObj;
试一试{
jObj=新的JSONObject();
JSONArray jArr=jObj.getJSONArray(结果);
for(int i=0;i
您可以在Android中使用GSON库进行JSON解析。您可以通过以下方式解析此JSON:
List<YourObject> listOfYourObject = new Gson().fromJson(results, new TypeToken<List<YourObject>>() {}.getType());
List listOfYourObject=new Gson().fromJson(结果,new TypeToken(){}.getType());
在YourObject中,准确地定义字段在响应中的方式,或者您可以使用@SerializedName注释并根据需要命名字段。您可以使用Android SDK中提供的JSONArray和JSONObject类。加载JSON字符串后,可以将其解析为:
JSONArray myJSONArray=new JSONArray(jsonString);
//jsonString contains your JSON loaded
//from the Internet probably.
for(int i=0;i<myJSONArray.length();i++){
JSONObject obj=(JSONObject) myJSONArray.get(i);
//Now you can access your data here.
String individualID=obj.getString("IndividualID");//get value by key
String fullName=obj.getString("FullName");
//.... similarly get other values and assign them to your views accordingly.
}
JSONArray myJSONArray=新的JSONArray(jsonString);
//jsonString包含加载的JSON
//可能是从互联网上。
对于(int i=0;iIs),您的JSON将JSON数组([)作为根?@John,这也是一种JSON类型。正如我在JSON数据中看到的,第一个元素是JSONArray,但在您的注释代码中,您使用的是JSONObject。此外,在您的JSON数据中没有作为“结果”的元素,因为您正试图在注释代码中解析它。。。
JSONArray myJSONArray=new JSONArray(jsonString);
//jsonString contains your JSON loaded
//from the Internet probably.
for(int i=0;i<myJSONArray.length();i++){
JSONObject obj=(JSONObject) myJSONArray.get(i);
//Now you can access your data here.
String individualID=obj.getString("IndividualID");//get value by key
String fullName=obj.getString("FullName");
//.... similarly get other values and assign them to your views accordingly.
}