如何在Codeigniter中解析此JSON

时间:2017-09-22 11:44:54

标签: php jquery codeigniter

如何在Codeigniter中解析此JSON(这是ajax的响应)

   {"status":true,"data":{"order_detail":[{"id":"1","order_number":"123","customer_name":"Ramm","amount":"","delivery_date":"2017-09-29","order_status":"Out for Delivery","discount":"100","total_cloth":"","mobile":"1223323344","customer_address":"delhii","store_id":"1","payment_mode":"Cash","payment_status":"Paid"}],"cloth_detail":[{"id":"55","order_no":"123","cloth_type":"Men-Jeans ","cloth_color":"Red ","quantity":"37","treatment":"DryCleaning "},{"id":"56","order_no":"123","cloth_type":"Men-Jeans ","cloth_color":"Blue ","quantity":"232","treatment":"DryCleaning "},{"id":"57","order_no":"123","cloth_type":"Shirt ","cloth_color":"Red ","quantity":"560","treatment":"Washing "},{"id":"58","order_no":"123","cloth_type":"Shirt ","cloth_color":"Red ","quantity":"423","treatment":"Washing + Pressing "},{"id":"59","order_no":"123","cloth_type":"Pant ","cloth_color":"Blue ","quantity":"5435","treatment":"DryCleaning "}]}}

3 个答案:

答案 0 :(得分:1)

您可以使用JSON.parse内置函数。

var x = '{"status":true,"data":{"order_detail":[{"id":"1","order_number":"123","customer_name":"Ramm","amount":"","delivery_date":"2017-09-29","order_status":"Out for Delivery","discount":"100","total_cloth":"","mobile":"1223323344","customer_address":"delhii","store_id":"1","payment_mode":"Cash","payment_status":"Paid"}],"cloth_detail":[{"id":"55","order_no":"123","cloth_type":"Men-Jeans ","cloth_color":"Red ","quantity":"37","treatment":"DryCleaning "},{"id":"56","order_no":"123","cloth_type":"Men-Jeans ","cloth_color":"Blue ","quantity":"232","treatment":"DryCleaning "},{"id":"57","order_no":"123","cloth_type":"Shirt ","cloth_color":"Red ","quantity":"560","treatment":"Washing "},{"id":"58","order_no":"123","cloth_type":"Shirt ","cloth_color":"Red ","quantity":"423","treatment":"Washing + Pressing "},{"id":"59","order_no":"123","cloth_type":"Pant ","cloth_color":"Blue ","quantity":"5435","treatment":"DryCleaning "}]}}';

然后你可以解析

var myjson = JSON.parse(x);

然后你从JSON.parse获得一个对象

var status = myjson.status // you get true as per your json object

订单详情

var orderDetail = myjson.data.order_detail;

我希望这会对你有所帮助。

答案 1 :(得分:0)

在javascript中,您可以使用

解析json
/* **data** is response you recieved by ajax*/
  var json =  JSON.parse(data);

如果你想获取status数据

 var status = json.status;



var data =' {"status":true,"data":{"order_detail":[{"id":"1","order_number":"123","customer_name":"Ramm","amount":"","delivery_date":"2017-09-29","order_status":"Out for Delivery","discount":"100","total_cloth":"","mobile":"1223323344","customer_address":"delhii","store_id":"1","payment_mode":"Cash","payment_status":"Paid"}],"cloth_detail":[{"id":"55","order_no":"123","cloth_type":"Men-Jeans ","cloth_color":"Red ","quantity":"37","treatment":"DryCleaning "},{"id":"56","order_no":"123","cloth_type":"Men-Jeans ","cloth_color":"Blue ","quantity":"232","treatment":"DryCleaning "},{"id":"57","order_no":"123","cloth_type":"Shirt ","cloth_color":"Red ","quantity":"560","treatment":"Washing "},{"id":"58","order_no":"123","cloth_type":"Shirt ","cloth_color":"Red ","quantity":"423","treatment":"Washing + Pressing "},{"id":"59","order_no":"123","cloth_type":"Pant ","cloth_color":"Blue ","quantity":"5435","treatment":"DryCleaning "}]}}';
var json_data = JSON.parse(data);
/*console.log(json_data);*/

var status =  json_data.status;
console.log(status);
var order_det = json_data.data;
for (var k in order_det) {
  console.log(order_det[k]);
  /* if u want to get id */
  console.log(order_det[k][0].id);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

  

您可以使用JavaScript JSON.parse

执行此操作
var myArray_Var = JSON.parse(YourJSONString);