越来越不能转换为jsonarray json对象 [英] Getting cannot convert jsonarray to json object
问题描述
你好我正在开发使用MySQL数据库的应用程序,但是当我试图获取值并显示它,我得到以下错误。
5月2日至20日:48:33.021:W / System.err的(1723):org.json.JSONException:值[{3:影像\\ /BigBazaar.png ,2:Jayanagar的4块,outlet_name:大巴扎,1:大巴扎,0:1,outlet_image:影像\\ /BigBazaar.png outlet_location:Jayanagar的4块,outlet_id:1}]类型的org.json.JSONArray不能转换到的JSONObject
我也能够看到日志即输出;
5月2日至20日:48:33.380:I / TAG(1723):[{0:1,outlet_id:1,1: 大巴扎,outlet_name:大巴扎,2:Jayanagar的4块,outlet_location:Jayanagar的4块,3:影像\\ /BigBazaar.png,outlet_image 影像\\ /BigBazaar.png}]
这是我的code。
公共类StoreActivity延伸活动{
私人字符串mBaseUrl =http://192.168.1.5/Flutura/PHP/;
私人字符串mDataUrl = mBaseUrl +核心/数据/ android.data3.php
私人字符串mAssetsUrl = mBaseUrl +资产/;
私人字符串mRequest =出路;
私人字符串mOutletID =0; 私人字符串mRecommendedProducts =;
私人字符串mOutletDetails =;
私人共享preferences我的preFS; / **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.store);
我的preFS = this.getShared preferences(我的preFS,MODE_WORLD_READABLE);
mOutletID =我prefs.getString(outlet_id,0);
mOutletDetails =我的prefs.getString(outlet_details,{});
Log.v(outlet_details,我的prefs.getString(outlet_details,{}));
如果(mOutletDetails!={}){
setOutletData(mOutletDetails);
}
其他{
executeAjaxRequest();
}
} 私人无效executeAjaxRequest(){ ?请求=字符串的URL = mDataUrl + + + mRequest与& outlet_id =+ mOutletID;
Log.v(URL,URL); AsyncHttpClient的HttpClient =新AsyncHttpClient();
httpclient.get(网址,新AsyncHtt presponseHandler(){
@覆盖
公共无效的onSuccess(字符串响应){
setOutletData(响应);
Log.i(TAG,响应);
}
});
}
私人无效setOutletData(字符串响应){
尝试{
JSONObject的商店=新的JSONObject(响应); ImageView的store_avatar =(ImageView的)findViewById(R.id.store_avatar);
TextView的STORE_ID =(的TextView)findViewById(R.id.store_id);
TextView的STORE_NAME =(的TextView)findViewById(R.id.store_name);
TextView的store_loc =(的TextView)findViewById(R.id.store_location); 如果(store_avatar!= NULL){
/ *
诠释渣油;
渣油= getApplicationContext().getResources().getIdentifier(store.getString(\"outlet_image\").replaceAll(\".png\",\"\"), 可拉伸,org.flutura.recommendation);
store_avatar.setImageResource(渣油); * /
ImageDownloader imdload =新ImageDownloader();
imdload.setMode(ImageDownloader.Mode.CORRECT);
imdload.download(mAssetsUrl + store.getString(outlet_image),store_avatar); mOutletDetails = store.toString();
mRecommendedProducts = store.getString(recommended_products);
store_avatar.setClickable(真);
store_avatar.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
意图myIntent =新意图(StoreActivity.this,StoreMapActivity.class);
共享preferences我的preFS = getApplicationContext()getShared preferences(我的preFS,MODE_WORLD_READABLE)。
共享preferences.Editor prefsEditor =我的prefs.edit(); prefsEditor.putString(outlet_details,mOutletDetails);
prefsEditor.commit();
startActivity(myIntent);
}
});
} mOutletID = store.getString(outlet_id); 如果(STORE_ID!= NULL){
store_id.setText(mOutletID);
} 如果(STORE_NAME!= NULL){
store_name.setText(store.getString(outlet_desc));
} 如果(store_loc!= NULL){
store_loc.setText(store.getString(outlet_loc));
} 按钮recommended_products_button =(按钮)findViewById(R.id.recommended_products_button);
recommended_products_button.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){ //加载推荐产品屏幕
意图myIntent =新意图(StoreActivity.this,RecommendedProductsListActivity.class);
共享preferences我的preFS = getApplicationContext()getShared preferences(我的preFS,MODE_WORLD_READABLE)。
共享preferences.Editor prefsEditor =我的prefs.edit(); prefsEditor.putString(outlet_id,mOutletID);
prefsEditor.putString(recommended_products,mRecommendedProducts);
prefsEditor.commit();
startActivity(myIntent);
} }); 按钮category_wise_sales_button =(按钮)findViewById(R.id.category_wise_sales_button);
category_wise_sales_button.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){ //加载推荐产品屏幕
意图myIntent =新意图(StoreActivity.this,CategoryWiseSalesActivity.class);
共享preferences我的preFS = getApplicationContext()getShared preferences(我的preFS,MODE_WORLD_READABLE)。
共享preferences.Editor prefsEditor =我的prefs.edit(); prefsEditor.putString(outlet_id,mOutletID);
prefsEditor.commit();
startActivity(myIntent);
} });
}
赶上(JSONException E){
e.printStackTrace();
}
赶上(NullPointerException异常五){
e.printStackTrace();
}
}}
这是我的PHP code。
< PHP
(0)使用error_reporting;// $ URL = $ _GET ['URL'];
// $ MR = $ _GET ['mRequest'];
$ MOID = $ _GET ['mOutletId'];
// $ mloc = $ _GET ['mLocation'];
//连接到数据库
$用户=根;
$ PSWD =;
$ DB =recommendations_db;
$主机=localhost的;
$康恩= mysql_connect($主机,$用户,$ PSWD);
mysql_select_db($分贝);
//如果($先生=='店'和;&安培; $ mloc ='全部'){
$查询=选择outlet_id,outlet_name,outlet_location,outlet_image FROM网点WHERE outlet_id ='$ MOID';
$结果= mysql_query($查询)或死亡(无法验证用户,因为:。mysql_error());//而($行= mysql_fetch_array($结果))
// {
$输出[] = mysql_fetch_array($结果);
//}
打印(json_en code($输出));
?>
谁能告诉我什么是错误的,因为我是在紧张的日程和需要完成今天这个。
code搜索按钮。
search_button.setClickable(真);
search_button.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
。字符串outlet_no = outlet_id.getText()的toString();
如果(!outlet_no.isEmpty()){
@燮pressWarnings(德precation)
共享preferences我的preFS = getApplicationContext()getShared preferences(我的preFS,MODE_WORLD_READABLE)。
共享preferences.Editor prefsEditor =我的prefs.edit(); prefsEditor.putString(outlet_id,outlet_no);
prefsEditor.commit(); 意图myIntent =新意图(HomeActivity.this,StoreActivity.class);
startActivity(myIntent);
HomeActivity.this.startActivity(myIntent);
}
其他{
Toast.makeText(getApplicationContext(),请输入一个出口ID,Toast.LENGTH_SHORT);
}
}
});
获取不能转换为jsonarray JSON对象
块引用>由于您是从服务器获取JSONArray代替的JSONObject所以只通过转换字符串作为jsonArray改变你的code:
JSONArray jsonArr =新JSONArray(响应); //<<<转换为jsonarray//来自JsonArray提取jsonObejct
JSONObject的商店= jsonArr.getJSONObject(0);而不是
的JSONObject店=新的JSONObject(响应);
Hi i am developing an app which uses mysql db but when i try to get the values and display it i get the following error.
02-20 05:48:33.021: W/System.err(1723): org.json.JSONException: Value [{"3":"images\/BigBazaar.png","2":"Jayanagar 4th Block","outlet_name":"Big Bazaar","1":"Big Bazaar","0":"1","outlet_image":"images\/BigBazaar.png","outlet_location":"Jayanagar 4th Block","outlet_id":"1"}] of type org.json.JSONArray cannot be converted to JSONObject
I am also able to see the output in log ie;
02-20 05:48:33.380: I/TAG(1723): [{"0":"1","outlet_id":"1","1":"Big Bazaar","outlet_name":"Big Bazaar","2":"Jayanagar 4th Block","outlet_location":"Jayanagar 4th Block","3":"images\/BigBazaar.png","outlet_image":"images\/BigBazaar.png"}]
This is my code.
public class StoreActivity extends Activity { private String mBaseUrl="http://192.168.1.5/Flutura/PHP/"; private String mDataUrl=mBaseUrl+"Core/Data/android.data3.php"; private String mAssetsUrl=mBaseUrl+"Assets/"; private String mRequest="outlet"; private String mOutletID="0"; private String mRecommendedProducts=""; private String mOutletDetails=""; private SharedPreferences myPrefs ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.store); myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE); mOutletID = myPrefs.getString("outlet_id", "0"); mOutletDetails = myPrefs.getString("outlet_details","{}"); Log.v("outlet_details",myPrefs.getString("outlet_details","{}")); if(mOutletDetails != "{}"){ setOutletData(mOutletDetails); } else{ executeAjaxRequest(); } } private void executeAjaxRequest(){ String url = mDataUrl+"?request="+mRequest+"&outlet_id="+mOutletID; Log.v("url",url); AsyncHttpClient httpclient = new AsyncHttpClient(); httpclient.get(url, new AsyncHttpResponseHandler() { @Override public void onSuccess(String response) { setOutletData(response); Log.i("TAG",response); } }); } private void setOutletData(String response){ try{ JSONObject store = new JSONObject(response); ImageView store_avatar = (ImageView) findViewById(R.id.store_avatar); TextView store_id = (TextView) findViewById(R.id.store_id); TextView store_name = (TextView) findViewById(R.id.store_name); TextView store_loc = (TextView) findViewById(R.id.store_location); if(store_avatar != null){ /* int resid; resid = getApplicationContext().getResources().getIdentifier(store.getString("outlet_image").replaceAll(".png",""), "drawable", "org.flutura.recommendation"); store_avatar.setImageResource(resid);*/ ImageDownloader imdload = new ImageDownloader(); imdload.setMode(ImageDownloader.Mode.CORRECT); imdload.download(mAssetsUrl+store.getString("outlet_image"),store_avatar ); mOutletDetails = store.toString(); mRecommendedProducts = store.getString("recommended_products"); store_avatar.setClickable(true); store_avatar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent myIntent = new Intent(StoreActivity.this,StoreMapActivity.class); SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE); SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putString("outlet_details", mOutletDetails); prefsEditor.commit(); startActivity(myIntent); } }); } mOutletID = store.getString("outlet_id"); if(store_id != null){ store_id.setText(mOutletID); } if(store_name != null){ store_name.setText(store.getString("outlet_desc")); } if(store_loc != null){ store_loc.setText(store.getString("outlet_loc")); } Button recommended_products_button = (Button) findViewById(R.id.recommended_products_button); recommended_products_button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ // Load the recommended products screen Intent myIntent = new Intent(StoreActivity.this,RecommendedProductsListActivity.class); SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE); SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putString("outlet_id",mOutletID); prefsEditor.putString("recommended_products", mRecommendedProducts); prefsEditor.commit(); startActivity(myIntent); } }); Button category_wise_sales_button = (Button) findViewById(R.id.category_wise_sales_button); category_wise_sales_button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ // Load the recommended products screen Intent myIntent = new Intent(StoreActivity.this,CategoryWiseSalesActivity.class); SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE); SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putString("outlet_id",mOutletID); prefsEditor.commit(); startActivity(myIntent); } }); } catch(JSONException e){ e.printStackTrace(); } catch(NullPointerException e){ e.printStackTrace(); } } }
This is my php code.
<?php error_reporting(0); //$url = $_GET['url']; //$mR = $_GET['mRequest']; $mOid = $_GET['mOutletId']; //$mloc = $_GET['mLocation']; //connect to the db $user = "root"; $pswd = ""; $db = "recommendations_db"; $host = "localhost"; $conn = mysql_connect($host, $user, $pswd); mysql_select_db($db); //if($mR == 'outlets' && $mloc = 'all'){ $query = "SELECT outlet_id,outlet_name,outlet_location,outlet_image FROM outlets WHERE outlet_id = '$mOid'"; $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error()); //while($row = mysql_fetch_array($result)) //{ $output[] = mysql_fetch_array($result); //} print( json_encode($output)); ?>
Can anyone tell me what is wrong as i am on a tight schedule and need to finish this today.
Code for search button.
search_button.setClickable(true); search_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String outlet_no = outlet_id.getText().toString(); if(!outlet_no.isEmpty()){ @SuppressWarnings("deprecation") SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE); SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putString("outlet_id", outlet_no); prefsEditor.commit(); Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class); startActivity(myIntent); HomeActivity.this.startActivity(myIntent); } else{ Toast.makeText(getApplicationContext(), "Please enter an outlet id", Toast.LENGTH_SHORT); } } });
解决方案Getting cannot convert jsonarray to json object
because your are getting JSONArray instead of JSONObject from server so just change your code by converting String to jsonArray as :
JSONArray jsonArr = new JSONArray(response); //<<< convert to jsonarray // extract jsonObejct from JsonArray JSONObject store = jsonArr.getJSONObject(0);
instead of
JSONObject store = new JSONObject(response);
这篇关于越来越不能转换为jsonarray json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!