删除 json 对象中索引为 0 的额外大括号

Posted

技术标签:

【中文标题】删除 json 对象中索引为 0 的额外大括号【英文标题】:Remove of extra curly brackets with 0 index in json object 【发布时间】:2017-05-18 19:36:49 【问题描述】:

这里我想去掉json对象中的一个大括号和一个零。

我尝试了很多,但没有成功。

下面我贴出我的代码,请看一下。


  "responseCode": 200,
  "status": "success",
  "statusReason": "Home Details",
  "result": [
    
      "branch_details": 
        "0":   //this 0 index and one curly bracket
          "company_company_id": "12345",
          "company_name": "AVIS",
          "company_image": "uploads/avis.png",
          "company_gps": "12.3499,39.41454",
          "company_address": "dfdfhf",
          "company_phone": "8951177685",
          "company_rating": "0",
          "branch_id": "1234",
          "branch_name": "avis1",
          "branch_image": "uploads/avis.png",
          "branch_gps": "12.3499,39.41454",
          "branch_address": "eredfdf",
          "branch_phone": "6745745456465",
          "branch_rating": "0"
        ,//this curly bracket
        "car_details": [
          
            "car_id": "123",
            "name": "audi",

          ,
          
            "car_id": "14782",
            "name": "bmw",

          ,
          
            "car_id": "14781",
            "name": "nano",

          ,
          
            "car_id": "14783",
            "name": "bmw",

          
        ]
      
    ,
    
      "branch_details": 
        "0": //this 0 index and one curly bracket
          "company_company_id": "12345",
          "company_name": "AVIS",
          "company_image": "uploads/avis.png",
          "company_gps": "12.3499,39.41454",
          "company_address": "dfdfhf",
          "company_phone": "8951177685",
          "company_rating": "0",
          "branch_id": "1478",
          "branch_name": "hertz1",
          "branch_image": "uploads/company2.png",
          "branch_gps": "12.3499,39.41454",
          "branch_address": "sfgsdfg",
          "branch_phone": "sdfgsdfgsdgf",
          "branch_rating": "0"
        ,//this curly bracket
        "car_details": []
      
    
  ]

这里我想删除一对大括号和一个零索引,但我无法做到。

代码

此代码在 for each 中,我发布了其中的一部分。

$this->db->select("IFNULL(company.company_id,'') as company_company_id,IFNULL(company.name,'') as company_name,IFNULL(company.image,'') as company_image,IFNULL(company.gps,'') as company_gps,IFNULL(company.address,'')as company_address,IFNULL(company.phone,'')as company_phone,(select IFNULL(avg(rating),0) from company_rating where user_id='$user_id'  and delete_status ='false' )as company_rating,IFNULL(branch_id,'') as branch_id,IFNULL(branch.name,'') as branch_name,IFNULL(branch.image,'') as branch_image,IFNULL(branch.gps,'') as branch_gps,IFNULL(branch.address,'')as branch_address,IFNULL(branch.phone,'')as branch_phone,(select IFNULL(avg(rating),0) from branch_rating where user_id='$user_id'  and delete_status ='false' )as branch_rating");

$this->db->from("branch");
$this->db->join("company","branch.company_id=company.company_id");
$this->db->where("branch.branch_id",$branch_id);
$this->db->where("branch.delete_status" , "false");
$this->db->where("company.delete_status" , "false");
$tempquery111 = $this->db->get();

$temp1['branch_details'] = $tempquery111->result();


$this->db->select("IFNULL(car_id,'') as car_id,IFNULL(name,'') as name,IFNULL(image,'') as image,IFNULL(model,'') as model,IFNULL(price,'')as price,IFNULL(year,'')as year,IFNULL(type,'')as type,IFNULL(`no_of_seats`,'')as `no_of_seats`,IFNULL(`gear_type`,'') as `gear_type`,IFNULL(color,'') as color,insurance,status,IFNULL(description,'') as description,(select IFNULL(avg(rating),0) from car_rating where user_id='$user_id'  and delete_status ='false' )as rating");

$car_details = $this->db->get_where("cars",array("cars.branch_id" =>$branch_id ,"delete_status" => "false"));
$temp1['branch_details']['car_details'] = $car_details->result(); 

array_push($tempquery11,$temp1);

任何人都可以提出一些建议吗?

【问题讨论】:

那么$tempquery11 是在哪里创建的? 我正在做数组推送,这里有一些要求..@RiggsFolly 我会说你要删除的"0": .... 实际上是第一个分支细节的数组索引。我猜如果您在数组中有另一个分支,那么它将在前面加上 "1": .... 我不知道这种语言,所以无法提供详细帮助,但建议您最好接受这一点并专注于JSON,可以很容易地以您想要的方式进行预处理以提取分支详细信息。 【参考方案1】:

在codeigniter中查询db时,result()会返回一个数组(即使只有一个结果),row()会返回一行。

尝试将$car_details->result(); 更改为$car_details->row();

抱歉,我误读了 json 对象。我应该说改变

$tempquery111->result();$tempquery111->row();

编辑:

尝试像这样设置你的数组。

如果它适用于 company_company_id,它将适用于整个部分,您只需对每个部分执行相同的操作。

$temp1['branch_details']['company_company_id'] = $tempquery111->result()->company_company_id;

我确信有更好的方法可以做到这一点,但对于我的生活,我现在想不出它。

【讨论】:

但它显示了一些错误,例如 . 致命错误:在中不能使用stdClass类型的对象作为数组 @sradha 嗯 - 我已经更新了我的答案,它应该可以解决问题,但我认为这不是最好的方法。希望其他人可以参与进来。

以上是关于删除 json 对象中索引为 0 的额外大括号的主要内容,如果未能解决你的问题,请参考以下文章

如何从 JSON 数组数据输出中删除大括号 []?

string.format格式化字符串中转义大括号“{}”

在转换为具有拆分方向的 json 之前从数据框中删除索引

PHP怎么生成去掉中括号的JSON

Resharper 将顶部大括号移动到 if 语句旁边。怎么上新线?

jmeter--正则提取json串中一个大括号对象里指定字段值的的另一个字段的值