Android 应用程序 JSON 正在解析
Android App JSON Parsing
我正在尝试解析从我的 android 应用程序中链接的 URL 收到的 JSON。我想将此数据输出到我的应用程序中的 TextViews。源代码如下。
目前 JSON 在应用程序中的显示方式
[
{
"IndividualID": 1,
"FullName": "Sean Kelly",
"DOB": "07/06/1987",
"MedicalHistory": "Ulcerative Colitis",
"Medication": "Asacolon",
"Alergies": "Penicillin"
}
]
Fragment2.java(注释掉的部分是我尝试解析但没有显示的地方)
package ie.itsligo.medication;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment2 extends Fragment {
TextView txtId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2_layout, container, false);
new LongRunningGetIO().execute();
return view;
}
private class LongRunningGetIO extends AsyncTask <Void, Void, String> {
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("https://api.myjson.com/bins/363s3");
String text = null;
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
} catch (Exception e) {
return e.getLocalizedMessage();
}
return text;
}
protected void onPostExecute(String results) {
if (results!=null) {
txtId = (TextView) getView().findViewById(R.id.txtId);
txtId.setText(results);
/*JSONObject jObj;
try {
jObj = new JSONObject();
JSONArray jArr = jObj.getJSONArray(results);
for (int i=0; i < jArr.length(); i++){
JSONObject obj = jArr.getJSONObject(i);
String fullname = obj.getString("FullName");
txtId = (TextView) getView().findViewById(R.id.txtId);
txtId.setText(fullname+"");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}
}
}
您可以使用 GSON 库在 Android 中进行 JSON 解析。你可以这样解析这个JSON:
List<YourObject> listOfYourObject = new Gson().fromJson(results, new TypeToken<List<YourObject>>() {}.getType());
在 YourObject 中准确定义字段在响应中的位置,或者您可以使用 @SerializedName 批注并根据需要命名字段。
您可以使用 Android SDK 中提供的 JSON 数组和 JSON 对象 类。加载 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.
}
希望对您有所帮助。
我正在尝试解析从我的 android 应用程序中链接的 URL 收到的 JSON。我想将此数据输出到我的应用程序中的 TextViews。源代码如下。
目前 JSON 在应用程序中的显示方式
[
{
"IndividualID": 1,
"FullName": "Sean Kelly",
"DOB": "07/06/1987",
"MedicalHistory": "Ulcerative Colitis",
"Medication": "Asacolon",
"Alergies": "Penicillin"
}
]
Fragment2.java(注释掉的部分是我尝试解析但没有显示的地方)
package ie.itsligo.medication;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment2 extends Fragment {
TextView txtId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2_layout, container, false);
new LongRunningGetIO().execute();
return view;
}
private class LongRunningGetIO extends AsyncTask <Void, Void, String> {
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("https://api.myjson.com/bins/363s3");
String text = null;
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
} catch (Exception e) {
return e.getLocalizedMessage();
}
return text;
}
protected void onPostExecute(String results) {
if (results!=null) {
txtId = (TextView) getView().findViewById(R.id.txtId);
txtId.setText(results);
/*JSONObject jObj;
try {
jObj = new JSONObject();
JSONArray jArr = jObj.getJSONArray(results);
for (int i=0; i < jArr.length(); i++){
JSONObject obj = jArr.getJSONObject(i);
String fullname = obj.getString("FullName");
txtId = (TextView) getView().findViewById(R.id.txtId);
txtId.setText(fullname+"");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}
}
}
您可以使用 GSON 库在 Android 中进行 JSON 解析。你可以这样解析这个JSON:
List<YourObject> listOfYourObject = new Gson().fromJson(results, new TypeToken<List<YourObject>>() {}.getType());
在 YourObject 中准确定义字段在响应中的位置,或者您可以使用 @SerializedName 批注并根据需要命名字段。
您可以使用 Android SDK 中提供的 JSON 数组和 JSON 对象 类。加载 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.
}
希望对您有所帮助。