如何从 json 数组中获取 Json 对象?
How to get Json Object from json Array?
我想从这个 json 文件中获取 promotion_price 如何获取它。我已将我的代码粘贴到我试图获取 promotion_price 的位置,但我每次都得到空值,请告诉我如何获取这些值的代码。
{
"current_page": 1,
"data": [
{
"id": 19,
"sku": "7788",
"image": "https://hdmart.pk/data/product/Head And Shoulders Scalp Care 360 Ml.jpg",
"brand_id": 1,
"vendor_id": 1,
"price": 420,
"cost": 420,
"stock": 0,
"sold": 1,
"type": 1,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 1,
"view": 15,
"alias": "kashmir-oil",
"date_lastview": "2020-02-27 15:38:47",
"date_available": null,
"created_at": "2020-02-21 21:32:04",
"updated_at": "2020-02-27 15:38:47",
"name": "Head And Shoulders Scalp Care 360 Ml",
"keyword": "Head And Shoulders Scalp Care 360 Ml",
"description": "Head And Shoulders Scalp Care 360 Ml",
"content": "kashmir oil is best",
"descriptions": [
{
"product_id": 19,
"lang": "en",
"name": "Head And Shoulders Scalp Care 360 Ml",
"keyword": "Head And Shoulders Scalp Care 360 Ml",
"description": "Head And Shoulders Scalp Care 360 Ml",
"content": "kashmir oil is best"
}
],
"promotion_price": null
},
{
"id": 20,
"sku": "123",
"image": "https://hdmart.pk/data/product/Ponds Triple Vitamin 200ml.png",
"brand_id": 7,
"vendor_id": 1,
"price": 260,
"cost": 260,
"stock": 0,
"sold": 10,
"type": 1,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 1,
"view": 12,
"alias": "ponds-lotion",
"date_lastview": "2020-02-27 20:58:35",
"date_available": null,
"created_at": "2020-02-22 00:01:09",
"updated_at": "2020-02-27 20:58:35",
"name": "Ponds Triple Vitamin 200ml",
"keyword": "Ponds Triple Vitamin 200ml",
"description": "Ponds Triple Vitamin Moisturising Lotion Silky Smooth Skin 200ML",
"content": "Ponds Triple Vitamin 200ml ",
"descriptions": [
{
"product_id": 20,
"lang": "en",
"name": "Ponds Triple Vitamin 200ml",
"keyword": "Ponds Triple Vitamin 200ml",
"description": "Ponds Triple Vitamin Moisturising Lotion Silky Smooth Skin 200ML",
"content": "Ponds Triple Vitamin 200ml "
}
],
"promotion_price": {
"product_id": 20,
"price_promotion": 255,
"date_start": null,
"date_end": null,
"status_promotion": 1,
"created_at": "2020-02-27 15:27:24",
"updated_at": "2020-02-27 15:27:24"
}
},
{
"id": 21,
"sku": "0001",
"image": "https://hdmart.pk/data/product/Tissue Papers.png",
"brand_id": 3,
"vendor_id": 1,
"price": 120,
"cost": 120,
"stock": 0,
"sold": 0,
"type": 1,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 1,
"view": 11,
"alias": "fyfy",
"date_lastview": "2020-02-27 14:40:38",
"date_available": null,
"created_at": "2020-02-22 14:37:44",
"updated_at": "2020-02-27 14:42:40",
"name": "Rose Petal Pop-Up Ultra Soft",
"keyword": "Tissue Paper",
"description": "1 Pack 220 Sheets",
"content": "gvjhvh",
"descriptions": [
{
"product_id": 21,
"lang": "en",
"name": "Rose Petal Pop-Up Ultra Soft",
"keyword": "Tissue Paper",
"description": "1 Pack 220 Sheets",
"content": "gvjhvh"
}
],
"promotion_price": {
"product_id": 21,
"price_promotion": 120,
"date_start": "2020-02-27 00:00:00",
"date_end": "2020-02-29 00:00:00",
"status_promotion": 1,
"created_at": "2020-02-27 23:27:02",
"updated_at": "2020-02-27 23:27:02"
}
},
{
"id": 22,
"sku": "0004",
"image": "https://hdmart.pk/data/product/Head And Shoulders Anti-Hairfall 360 Ml.jpg",
"brand_id": 0,
"vendor_id": 0,
"price": 420,
"cost": 420,
"stock": 3,
"sold": 0,
"type": 1,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 0,
"view": 2,
"alias": "head-1",
"date_lastview": "2020-02-27 20:58:30",
"date_available": null,
"created_at": "2020-02-27 15:48:17",
"updated_at": "2020-02-27 20:58:30",
"name": "Head And Shoulders Anti-Hairfall 360 Ml",
"keyword": "Head And Shoulders Anti-Hairfall 360 Ml",
"description": "Head And Shoulders Anti-Hairfall 360 Ml",
"content": "Head And Shoulders Anti-Hairfall 360 Ml",
"descriptions": [
{
"product_id": 22,
"lang": "en",
"name": "Head And Shoulders Anti-Hairfall 360 Ml",
"keyword": "Head And Shoulders Anti-Hairfall 360 Ml",
"description": "Head And Shoulders Anti-Hairfall 360 Ml",
"content": "Head And Shoulders Anti-Hairfall 360 Ml"
}
],
"promotion_price": null
},
{
"id": 23,
"sku": "0005",
"image": "https://hdmart.pk/data/product/Headand Shoulders Classic Clean 360 Ml.jpg",
"brand_id": 7,
"vendor_id": 1,
"price": 420,
"cost": 420,
"stock": 3,
"sold": 0,
"type": 0,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 0,
"view": 2,
"alias": "sf",
"date_lastview": "2020-02-27 20:59:25",
"date_available": null,
"created_at": "2020-02-27 15:53:21",
"updated_at": "2020-02-27 20:59:25",
"name": "Headand Shoulders Classic Clean 360 Ml",
"keyword": "Headand Shoulders Classic Clean 360 Ml",
"description": "Headand Shoulders Classic Clean 360 Ml",
"content": "Headand Shoulders Classic Clean 360 Ml",
"descriptions": [
{
"product_id": 23,
"lang": "en",
"name": "Headand Shoulders Classic Clean 360 Ml",
"keyword": "Headand Shoulders Classic Clean 360 Ml",
"description": "Headand Shoulders Classic Clean 360 Ml",
"content": "Headand Shoulders Classic Clean 360 Ml"
}
],
"promotion_price": null
}
Android代码:
private void jsonRequestForOffers() {
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Toast.makeText(getContext(), response.toString(), Toast.LENGTH_SHORT).show();
try {
JSONArray data = response
.getJSONArray("data");
for (int i=0;i<data.length();i++){
JSONObject jOB = data.getJSONObject(i);
Anime anime = new Anime();
String product_name = jOB.getString("name");
anime.setName(product_name);
String product_price = jOB.getString("price");
anime.setPrice("RS "+product_price);
String product_Cut_price = jOB.getString("price");
anime.setPriceCut("RS "+product_Cut_price);
// JSONObject jsonObject = data.getJSONObject(1);
// anime.setPriceCut("RS "+jsonObject.getString("price_promotion"));
String product_img = jOB.getString("image");
anime.setImgUrlOfferProduct(product_img);
lstAnime.add(anime);
// String promotion = jOB.getString("price_promotion");
// Toast.makeText(getContext(), promotion, Toast.LENGTH_SHORT).show();
// Toast.makeText(getContext(), product_price, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
setUpRecyclerView(lstAnime);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "Error" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jsonObjectRequest);
}
尝试使用此代码从 JSONArray 获取 JSONObject
try{
JSONObject jsonObject=new JSONObject(response.body());
JSONArray array=jsonObject.getJSONArray("data");
for(int j=0;j<array.length();j++){
JSONObject object=array.getJSONObject(j);
JSONObject promotionPrice =object.getJSONObject("promotion_price");
Log.i(TAG, "initView: promotionPrice "+promotionPrice);
String id=object.optString("id");
}
}catch (Exception exception){
exception.printStackTrace();
}
首先创建 POJO Class 使用 getter 和 setter 获取促销价格,然后从 JSONObject 获取 JSONArray 并访问促销价格。
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = jsonObject.getJSONArray("data"); // Declare Model Class of array element
for(int i=0;i<jsonArray.size();i++)
{
PromotionalPrice promotionalPrice = jsonArray.get(i).getPromotionalPrices();
Log.d(TAG,promotionalPrice.price_promotion.toString());
}
您的 JSON 响应有 null
个 promotion_price
值。我修改了您的答案以解析 promotion_price
对象并为此添加了空检查。试试这个代码
private void jsonRequestForOffers() {
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Toast.makeText(getContext(), response.toString(), Toast.LENGTH_SHORT).show();
try {
JSONArray data = response
.getJSONArray("data");
for (int i=0;i<data.length();i++){
JSONObject jOB = data.getJSONObject(i);
Anime anime = new Anime();
String product_name = jOB.getString("name");
anime.setName(product_name);
String product_price = jOB.getString("price");
anime.setPrice("RS "+product_price);
String product_Cut_price = jOB.getString("price");
anime.setPriceCut("RS "+product_Cut_price);
JSONObject jsonObject = data.getJSONObject(i);
JSONObject promotionObject = jsonObject.getJSONObject("promotion_price"); // this line was missing
if(promotionObject != null){
anime.setPriceCut("RS "+jsonObject.getString("price_promotion"));
}
String product_img = jOB.getString("image");
anime.setImgUrlOfferProduct(product_img);
lstAnime.add(anime);
// String promotion = jOB.getString("price_promotion");
// Toast.makeText(getContext(), promotion, Toast.LENGTH_SHORT).show();
// Toast.makeText(getContext(), product_price, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
setUpRecyclerView(lstAnime);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "Error" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jsonObjectRequest);
}
我想从这个 json 文件中获取 promotion_price 如何获取它。我已将我的代码粘贴到我试图获取 promotion_price 的位置,但我每次都得到空值,请告诉我如何获取这些值的代码。
{
"current_page": 1,
"data": [
{
"id": 19,
"sku": "7788",
"image": "https://hdmart.pk/data/product/Head And Shoulders Scalp Care 360 Ml.jpg",
"brand_id": 1,
"vendor_id": 1,
"price": 420,
"cost": 420,
"stock": 0,
"sold": 1,
"type": 1,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 1,
"view": 15,
"alias": "kashmir-oil",
"date_lastview": "2020-02-27 15:38:47",
"date_available": null,
"created_at": "2020-02-21 21:32:04",
"updated_at": "2020-02-27 15:38:47",
"name": "Head And Shoulders Scalp Care 360 Ml",
"keyword": "Head And Shoulders Scalp Care 360 Ml",
"description": "Head And Shoulders Scalp Care 360 Ml",
"content": "kashmir oil is best",
"descriptions": [
{
"product_id": 19,
"lang": "en",
"name": "Head And Shoulders Scalp Care 360 Ml",
"keyword": "Head And Shoulders Scalp Care 360 Ml",
"description": "Head And Shoulders Scalp Care 360 Ml",
"content": "kashmir oil is best"
}
],
"promotion_price": null
},
{
"id": 20,
"sku": "123",
"image": "https://hdmart.pk/data/product/Ponds Triple Vitamin 200ml.png",
"brand_id": 7,
"vendor_id": 1,
"price": 260,
"cost": 260,
"stock": 0,
"sold": 10,
"type": 1,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 1,
"view": 12,
"alias": "ponds-lotion",
"date_lastview": "2020-02-27 20:58:35",
"date_available": null,
"created_at": "2020-02-22 00:01:09",
"updated_at": "2020-02-27 20:58:35",
"name": "Ponds Triple Vitamin 200ml",
"keyword": "Ponds Triple Vitamin 200ml",
"description": "Ponds Triple Vitamin Moisturising Lotion Silky Smooth Skin 200ML",
"content": "Ponds Triple Vitamin 200ml ",
"descriptions": [
{
"product_id": 20,
"lang": "en",
"name": "Ponds Triple Vitamin 200ml",
"keyword": "Ponds Triple Vitamin 200ml",
"description": "Ponds Triple Vitamin Moisturising Lotion Silky Smooth Skin 200ML",
"content": "Ponds Triple Vitamin 200ml "
}
],
"promotion_price": {
"product_id": 20,
"price_promotion": 255,
"date_start": null,
"date_end": null,
"status_promotion": 1,
"created_at": "2020-02-27 15:27:24",
"updated_at": "2020-02-27 15:27:24"
}
},
{
"id": 21,
"sku": "0001",
"image": "https://hdmart.pk/data/product/Tissue Papers.png",
"brand_id": 3,
"vendor_id": 1,
"price": 120,
"cost": 120,
"stock": 0,
"sold": 0,
"type": 1,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 1,
"view": 11,
"alias": "fyfy",
"date_lastview": "2020-02-27 14:40:38",
"date_available": null,
"created_at": "2020-02-22 14:37:44",
"updated_at": "2020-02-27 14:42:40",
"name": "Rose Petal Pop-Up Ultra Soft",
"keyword": "Tissue Paper",
"description": "1 Pack 220 Sheets",
"content": "gvjhvh",
"descriptions": [
{
"product_id": 21,
"lang": "en",
"name": "Rose Petal Pop-Up Ultra Soft",
"keyword": "Tissue Paper",
"description": "1 Pack 220 Sheets",
"content": "gvjhvh"
}
],
"promotion_price": {
"product_id": 21,
"price_promotion": 120,
"date_start": "2020-02-27 00:00:00",
"date_end": "2020-02-29 00:00:00",
"status_promotion": 1,
"created_at": "2020-02-27 23:27:02",
"updated_at": "2020-02-27 23:27:02"
}
},
{
"id": 22,
"sku": "0004",
"image": "https://hdmart.pk/data/product/Head And Shoulders Anti-Hairfall 360 Ml.jpg",
"brand_id": 0,
"vendor_id": 0,
"price": 420,
"cost": 420,
"stock": 3,
"sold": 0,
"type": 1,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 0,
"view": 2,
"alias": "head-1",
"date_lastview": "2020-02-27 20:58:30",
"date_available": null,
"created_at": "2020-02-27 15:48:17",
"updated_at": "2020-02-27 20:58:30",
"name": "Head And Shoulders Anti-Hairfall 360 Ml",
"keyword": "Head And Shoulders Anti-Hairfall 360 Ml",
"description": "Head And Shoulders Anti-Hairfall 360 Ml",
"content": "Head And Shoulders Anti-Hairfall 360 Ml",
"descriptions": [
{
"product_id": 22,
"lang": "en",
"name": "Head And Shoulders Anti-Hairfall 360 Ml",
"keyword": "Head And Shoulders Anti-Hairfall 360 Ml",
"description": "Head And Shoulders Anti-Hairfall 360 Ml",
"content": "Head And Shoulders Anti-Hairfall 360 Ml"
}
],
"promotion_price": null
},
{
"id": 23,
"sku": "0005",
"image": "https://hdmart.pk/data/product/Headand Shoulders Classic Clean 360 Ml.jpg",
"brand_id": 7,
"vendor_id": 1,
"price": 420,
"cost": 420,
"stock": 3,
"sold": 0,
"type": 0,
"kind": 0,
"virtual": 0,
"status": 1,
"sort": 0,
"view": 2,
"alias": "sf",
"date_lastview": "2020-02-27 20:59:25",
"date_available": null,
"created_at": "2020-02-27 15:53:21",
"updated_at": "2020-02-27 20:59:25",
"name": "Headand Shoulders Classic Clean 360 Ml",
"keyword": "Headand Shoulders Classic Clean 360 Ml",
"description": "Headand Shoulders Classic Clean 360 Ml",
"content": "Headand Shoulders Classic Clean 360 Ml",
"descriptions": [
{
"product_id": 23,
"lang": "en",
"name": "Headand Shoulders Classic Clean 360 Ml",
"keyword": "Headand Shoulders Classic Clean 360 Ml",
"description": "Headand Shoulders Classic Clean 360 Ml",
"content": "Headand Shoulders Classic Clean 360 Ml"
}
],
"promotion_price": null
}
Android代码:
private void jsonRequestForOffers() {
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Toast.makeText(getContext(), response.toString(), Toast.LENGTH_SHORT).show();
try {
JSONArray data = response
.getJSONArray("data");
for (int i=0;i<data.length();i++){
JSONObject jOB = data.getJSONObject(i);
Anime anime = new Anime();
String product_name = jOB.getString("name");
anime.setName(product_name);
String product_price = jOB.getString("price");
anime.setPrice("RS "+product_price);
String product_Cut_price = jOB.getString("price");
anime.setPriceCut("RS "+product_Cut_price);
// JSONObject jsonObject = data.getJSONObject(1);
// anime.setPriceCut("RS "+jsonObject.getString("price_promotion"));
String product_img = jOB.getString("image");
anime.setImgUrlOfferProduct(product_img);
lstAnime.add(anime);
// String promotion = jOB.getString("price_promotion");
// Toast.makeText(getContext(), promotion, Toast.LENGTH_SHORT).show();
// Toast.makeText(getContext(), product_price, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
setUpRecyclerView(lstAnime);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "Error" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jsonObjectRequest);
}
尝试使用此代码从 JSONArray 获取 JSONObject
try{
JSONObject jsonObject=new JSONObject(response.body());
JSONArray array=jsonObject.getJSONArray("data");
for(int j=0;j<array.length();j++){
JSONObject object=array.getJSONObject(j);
JSONObject promotionPrice =object.getJSONObject("promotion_price");
Log.i(TAG, "initView: promotionPrice "+promotionPrice);
String id=object.optString("id");
}
}catch (Exception exception){
exception.printStackTrace();
}
首先创建 POJO Class 使用 getter 和 setter 获取促销价格,然后从 JSONObject 获取 JSONArray 并访问促销价格。
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = jsonObject.getJSONArray("data"); // Declare Model Class of array element
for(int i=0;i<jsonArray.size();i++)
{
PromotionalPrice promotionalPrice = jsonArray.get(i).getPromotionalPrices();
Log.d(TAG,promotionalPrice.price_promotion.toString());
}
您的 JSON 响应有 null
个 promotion_price
值。我修改了您的答案以解析 promotion_price
对象并为此添加了空检查。试试这个代码
private void jsonRequestForOffers() {
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Toast.makeText(getContext(), response.toString(), Toast.LENGTH_SHORT).show();
try {
JSONArray data = response
.getJSONArray("data");
for (int i=0;i<data.length();i++){
JSONObject jOB = data.getJSONObject(i);
Anime anime = new Anime();
String product_name = jOB.getString("name");
anime.setName(product_name);
String product_price = jOB.getString("price");
anime.setPrice("RS "+product_price);
String product_Cut_price = jOB.getString("price");
anime.setPriceCut("RS "+product_Cut_price);
JSONObject jsonObject = data.getJSONObject(i);
JSONObject promotionObject = jsonObject.getJSONObject("promotion_price"); // this line was missing
if(promotionObject != null){
anime.setPriceCut("RS "+jsonObject.getString("price_promotion"));
}
String product_img = jOB.getString("image");
anime.setImgUrlOfferProduct(product_img);
lstAnime.add(anime);
// String promotion = jOB.getString("price_promotion");
// Toast.makeText(getContext(), promotion, Toast.LENGTH_SHORT).show();
// Toast.makeText(getContext(), product_price, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
setUpRecyclerView(lstAnime);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "Error" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jsonObjectRequest);
}