[英]How do I access each node from a JSON Array (Not JSON Object)?
我需要從具有JSON數組而不是JSON對象的URL下載數據,至少我的理解是這樣。
從URL檢索數據:
// JSONObject jsonObject=new JSONObject(ParsingDta);
JSONArray jsonArray = jsonObject.getJSONArray("Employee");
for (int i = 0; i < jsonArray.length(); i++) {
// JSONObject jsonObject1=jsonArray.getJSONObject(i);
String id = jsonObject1.getString("id").toString();
String name = jsonObject1.getString("name").toString();
String salary = jsonObject1.getString("salary").toString();
database.insertData(id, name, salary);
str += "\n Employee" + i + "\n name:" + name + "\n id:" + id + "\n salary:" + salary + "\n";
// textView1.setText(str);
}
上面有我猜的JSON對象,我需要JSON數組
[{"billedret":0,"class":"com.enteleki.sfa.botree.RetailerSummary","ctgcode":"G00" ...
等等..
如果您要從數據源接收JSON數組,則需要從接收到的數據創建JSON數組,而不是嘗試將數組變成對象。
嘗試創建一個新的JSON數組,如下所示。
JSONArray jsonArray = new JSONArray(ParsingData);
現在,應該根據收到的數據創建JSON數組,然后可以像使用普通JSON數組一樣使用它。
這就是我用來從網址獲取數據的方法
public void printRes(String res) throws JSONException {
try {
//the asyncTask returns string to this function
JSONArray arr = new JSONArray(res);
int t = arr.length();
nameArray = new String[t];
distArray = new String[t];
latArray = new String[t];
lonArray = new String[t];
pgr.setProgress(70);
list = new ArrayList<String>();
for (int i = 0; i < arr.length(); i++) {
pgr.setProgress(50);
nameArray[i] = (arr.getJSONObject(i).getString("name"));
distArray[i] = (arr.getJSONObject(i).getString("distance"));
latArray[i] = (arr.getJSONObject(i).getString("latitude"));
lonArray[i] = (arr.getJSONObject(i).getString("longitude"));
}
編輯
我使用asyncTask從網址下載數據
String latt = LatVal.getText().toString();
String lonn = LongVal.getText().toString();
// Create data variable for sending values to server
data = URLEncoder.encode("lat", "UTF-8") + "="
+ URLEncoder.encode(latt, "UTF-8");
data += "&" + URLEncoder.encode("longi", "UTF-8") + "="
+ URLEncoder.encode(lonn, "UTF-8");
data += "&" + URLEncoder.encode("radius", "UTF-8") + "="
+ URLEncoder.encode(Dis, "UTF-8");
text = "";
// Send data
new AsyncCaller().execute(data);
該字符串作為POST在線程AsyncCaller中發送
class AsyncCaller extends AsyncTask<String,String,String>
{
MainActivity obj=new MainActivity();
BufferedReader reader=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
}
@Override
protected String doInBackground(String...res) {
//this method will be running on background thread so don't update UI
// frome here
//do your long running http tasks here,you dont want to pass argument
//and u can access the parent class' variable url over here
//String ads=Arrays.toString(res);
try {
//Create connection
// Defined URL where to send data
URL url = new URL("**your URL**");
// Send POST data request
String data=res[0];
//String sub="lat=10.0243567&longi=76.3084289&radius=500";
//"lat=ben&longi=ascd&radius=500";
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
obj.text = sb.toString();
} catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {}
}
// Show response on activity
return obj.text;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
obj.printRes(result);//this contains the download data from the URL
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我使用此代碼在發布一些經度和緯度的同時從url下載數據...
U可以進行所需的更改以下載數據。
祝好運。
有一個用於處理這樣的Json Array的選項
String data="[{"billedret":0,"class":"com.enteleki.sfa.botree.RetailerSummary","ctgcode":"G00"....... your full json text}]";
JSONArray completeArray = new JSONArray(data);
for (int i=0;i<completeArray .length();i++){
JSONObject finalEntry=(JSONObject) completeArray .get(i);
// add your logic for read info from finalEntry jsonObjec.
}
我希望它適合你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.