我需要从具有JSON数组而不是JSON对象的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" ...
依旧......
答案 0 :(得分:0)
如果您从数据源接收JSON数组,则需要根据收到的数据创建JSON数组,而不是尝试将数组转换为对象。
尝试创建一个新的JSON数组,如下所示。
JSONArray jsonArray = new JSONArray(ParsingData);
现在应该根据收到的数据创建JSON数组,然后就像普通的JSON数组一样使用它。
答案 1 :(得分:0)
这是我用来从网址
获取数据的内容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);
此字符串在线程AsyncCaller
中作为POST发送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();
}
}
}
我使用此代码从网址下载数据,同时发布一些经度和纬度......
您可以进行所需的更改以下载数据,..
祝你好运。答案 2 :(得分:0)
有一个像这样处理Json数组的选项
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.
}
我希望它适合你。