我想知道如何将JSON数组内部解析为另一个JSON数组。我是android开发新手请帮助我并提前致谢。my web service url 我的代码: -
public class ProductsAsynTask extends AsyncTask<String,Void,Boolean>{
ProgressDialog dialog;
protected void onPreExecute(){
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, Please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
protected Boolean doInBackground(String... param){
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(param[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(data);
JSONArray jArray = jsonObject.getJSONArray("product");
for(int i = 0;i<jArray.length();i++){
JSONObject jsonObject1 = jArray.getJSONObject(i);
Products products = new Products();
products.setId(jsonObject1.getString("id"));
products.setName(jsonObject1.getString("name"));
products.setManufacturer(jsonObject1.getString("manufacturer"));
products.setModel(jsonObject1.getString("model"));
products.setReward(jsonObject1.getString("reward"));
products.setPoints(jsonObject1.getString("points"));
products.setImage(jsonObject1.getString("image"));
products.setPrice(jsonObject1.getString("price"));
products.setSpecial(jsonObject1.getString("special"));
JSONArray jArray1 = jArray.getJSONArray(i);
for(int j = 0;j<jArray1.length();j++){
JSONObject jsonObject2 = jArray1.getJSONObject(j);
products.setDiscounts_quantity(jsonObject2.getString("quantity"));
products.setDiscounts_price(jsonObject2.getString("price"));
}
JSONArray jArray2 = jArray.getJSONArray(i);
for(int j = 0;j<jArray2.length();j++){
JSONObject jsonObject2 = jArray2.getJSONObject(j);
products.setOptions_product_option_id(jsonObject2.getString("product_option_id"));
products.setOptions_option_id(jsonObject2.getString("option_id"));
products.setOptions_name(jsonObject2.getString("name"));
products.setOptions_type(jsonObject2.getString("type"));
JSONArray jsonArray3 = jArray2.getJSONArray(j);
for(int k = 0;k < jsonArray3.length();k++){
JSONObject jsonObject3 = jsonArray3.getJSONObject(k);
products.setOptions_option_value_product_option_value_id(jsonObject3.getString("product_option_value_id"));
products.setOptions_option_value_option_value_id(jsonObject3.getString("option_value_id"));
products.setOptions_option_value_name(jsonObject3.getString("name"));
products.setOptions_option_value_image(jsonObject3.getString("image"));
products.setOptions_option_value_price(jsonObject3.getString("price"));
products.setOptions_option_value_price_prefix(jsonObject3.getString("price_prefix"));
}
products.setOptions_required(jsonObject2.getString("required"));
}
products.setMinimum(jsonObject1.getString("minimum"));
products.setRating(jsonObject1.getString("rating"));
products.setDescription(jsonObject1.getString("description"));
JSONArray jsonArray = jArray.getJSONArray(i);
for(int j = 0; j < jsonArray.length();j++){
JSONObject jsonObject2 = jsonArray.getJSONObject(j);
products.setAttribute_groups_attribute_group_id(jsonObject2.getString("attribute_group_id"));
products.setAttribute_groups_name(jsonObject2.getString("name"));
JSONArray jsonArray1 = jsonArray.getJSONArray(j);
for(int k = 0; k < jsonArray1.length();k++){
JSONObject jsonObject3 = jsonArray1.getJSONObject(k);
products.setAttribute_groups_attribute_attribute_id(jsonObject3.getString("attribute_id"));
products.setAttribute_groups_attribute_name(jsonObject.getString("name"));
products.setAttribute_groups_attribute_text(jsonObject3.getString("text"));
}
}
productsList.add(products);
}
return true;
}
else if(status == 500){
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}catch (ClientProtocolException e){
Log.e("Error :",e.getMessage());
}catch (IOException e){
Log.e("Error :",e.getMessage());
}catch (JSONException e){
Log.e("Error :",e.getMessage());
}catch (Exception e){
Log.e("Error :",e.getMessage());
}
return false;
}
protected void onPostExecute(Boolean result){
dialog.cancel();
adapter.notifyDataSetChanged();
if(result == false){
Toast.makeText(MainActivity.this,"Data is not Parsed",Toast.LENGTH_LONG).show();
}
else{
ProductsAdapter adapter = new ProductsAdapter(getApplicationContext(),R.layout.content_main,productsList);
listView.setAdapter(adapter);
}
}
}
我是Android开发新手,请帮助我,并提前致谢。
答案 0 :(得分:1)
@Override
protected void onPostExecute(String res) {
try {
JSONArray jArray = new JSONArray(res.toString());
if (jArray.length() == 0) {
txtNoDataFound1.setVisibility(View.VISIBLE);
txtNoDataFound1.setText("No Data Found !!");
txtNoDataFound1.setGravity(Gravity.CENTER
| Gravity.CENTER_HORIZONTAL);
expListView.setVisibility(View.GONE);
} else {
ListHead.clear();
expListView.setVisibility(View.VISIBLE);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
getheadingid = json.getString("Headid");
listhead = new ListHeaderEntity(json.getString("Headid"),
json.getString("HeadServiceTitle"));
try {
JSONArray jArray1 = json.getJSONArray("subServiceData");
if (jArray1.length() == 0) {
} else {
listhead.setListitems(new ArrayList<CityEntity>());
for (int j = 0; j < jArray1.length(); j++) {
JSONObject json1 = jArray1.getJSONObject(j);
CityEntity listitem = new CityEntity();
listitem.setLblcityid(json1.getString("subid"));
listitem.setLblcityname(json1.getString("servicedetailTitle"));
listhead.getListitems().add(listitem);
}
}
} catch (Exception objEx) {
objEx.printStackTrace();
}
ListHead.add(listhead);
}
listAdapter = new ServiceExpandAdapter(
getActivity(), ListHead, expListView);
expListView.setAdapter(listAdapter);
res1 = null;
selectedService.clear();
PutUtility objClient1 = new PutUtility();
try {
res1 = objClient1.getData("url");
} catch (Exception objEx) {
objEx.printStackTrace();
}
JSONArray jArray1 = new JSONArray(res1.toString());
if (jArray1.length() == 0) {
} else {
for (int i = 0; i < jArray1.length(); i++) {
JSONObject json = jArray1.getJSONObject(i);
//selectedvalue.add(Integer.parseInt(json.getString("subservice_id")));
int in=Integer.parseInt(json.getString("subservice_id"));
selectedService.add(in);
}
}
}
mProgressDialog.dismiss();
} catch (Exception objEx) {
objEx.printStackTrace();
}
}
答案 1 :(得分:0)
要在对象中取另一个数组,您必须更改
JSONArray jArray1 = jArray.getJSONArray(i);
到
JSONArray jArray1 = jsonObject1.getJSONArray("discounts");
答案 2 :(得分:0)
在你的json响应中,“product”不是json数组,它是json对象。在doInBackground()
中更改为此。
protected Boolean doInBackground(String... param){
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(param[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if(status == 200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(data);
JSONObject jsonObject1 = jsonObject.getJSONObject("product");
Products products = new Products();
products.setId(jsonObject1.getString("id"));
products.setName(jsonObject1.getString("name"));
products.setManufacturer(jsonObject1.getString("manufacturer"));
products.setModel(jsonObject1.getString("model"));
products.setReward(jsonObject1.getString("reward"));
products.setPoints(jsonObject1.getString("points"));
products.setImage(jsonObject1.getString("image"));
products.setPrice(jsonObject1.getString("price"));
products.setSpecial(jsonObject1.getString("special"));
JSONArray jArray1 = jsonObject1.getJSONArray("discounts");
for(int j = 0;j<jArray1.length();j++){
JSONObject jsonObject2 = jArray1.getJSONObject(j);
products.setDiscounts_quantity(jsonObject2.getString("quantity"));
products.setDiscounts_price(jsonObject2.getString("price"));
}
JSONArray jArray2 = jsonObject1.getJSONArray("options");
for(int j = 0;j<jArray2.length();j++){
JSONObject jsonObject2 = jArray2.getJSONObject(j);
products.setOptions_product_option_id(jsonObject2.getString("product_option_value_id"));
products.setOptions_option_id(jsonObject2.getString("option_id"));
products.setOptions_name(jsonObject2.getString("name"));
products.setOptions_type(jsonObject2.getString("type"));
JSONArray jsonArray3 = jsonObject2.getJSONArray("option_value");
for(int k = 0;k < jsonArray3.length();k++){
JSONObject jsonObject3 = jsonArray3.getJSONObject(k);
products.setOptions_option_value_product_option_value_id(jsonObject3.getString("product_option_value_id"));
products.setOptions_option_value_option_value_id(jsonObject3.getString("option_value_id"));
products.setOptions_option_value_name(jsonObject3.getString("name"));
products.setOptions_option_value_image(jsonObject3.getString("image"));
products.setOptions_option_value_price(jsonObject3.getString("price"));
products.setOptions_option_value_price_prefix(jsonObject3.getString("price_prefix"));
}
products.setOptions_required(jsonObject2.getString("required"));
}
products.setMinimum(jsonObject1.getString("minimum"));
products.setRating(jsonObject1.getString("rating"));
products.setDescription(jsonObject1.getString("description"));
JSONArray jsonArray = jsonObject1.getJSONArray("attribute_groups");
for(int j = 0; j < jsonArray.length();j++){
JSONObject jsonObject2 = jsonArray.getJSONObject(j);
products.setAttribute_groups_attribute_group_id(jsonObject2.getString("attribute_group_id"));
products.setAttribute_groups_name(jsonObject2.getString("name"));
JSONArray jsonArray1 = jsonObject2.getJSONArray("attribute");
for(int k = 0; k < jsonArray1.length();k++){
JSONObject jsonObject3 = jsonArray1.getJSONObject(k);
products.setAttribute_groups_attribute_attribute_id(jsonObject3.getString("attribute_id"));
products.setAttribute_groups_attribute_name(jsonObject.getString("name"));
products.setAttribute_groups_attribute_text(jsonObject3.getString("text"));
}
}
productsList.add(products);
}
return true;
}
else if(status == 500){
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}catch (ClientProtocolException e){
Log.e("Error :",e.getMessage());
}catch (IOException e){
Log.e("Error :",e.getMessage());
}catch (JSONException e){
Log.e("Error :",e.getMessage());
}catch (Exception e){
Log.e("Error :",e.getMessage());
}
return false;
}
答案 3 :(得分:-1)
Json只会将json文件解析为一个json对象,然后你就可以引用它了。