dtvgo28624 2017-02-21 19:46
浏览 256
已采纳

从JSON中获取对象属性值

I am getting this returned to me:

{
"GROUP_CONCAT(DISTINCT(CONCAT(\"MAX(IF(pa.printer = \"\"\", printer, \"\"\", pa.printer, NULL)) AS \", printer)))":
 "MAX(IF(pa.printer = \"JP4-3\", pa.printer, NULL)) AS JP4-3,MAX(IF(pa.printer = \"JP4-1\", pa.printer, NULL)) AS JP4-1,MAX(IF(pa.printer = \"JP4-2\", pa.printer, NULL)) AS JP4-2,MAX(IF(pa.printer = \"B3\", pa.printer, NULL)) AS B3,MAX(IF(pa.printer = \"A2\", pa.printer, NULL)) AS A2"
    }

I want to get the value of this but just the string. I can't get it out of the {}. This should be so simple but cant find an answer anywhere.

BELOW IS JUST REFERENCE TO WHAT I AM DOING

The main goal is a MySQL pivot table. Takes 2 calls but I am OK with that.

$listQuery = PrintJob::select(DB::raw('GROUP_CONCAT(DISTINCT(CONCAT("MAX(IF(pa.printer = """, printer, """, pa.printer, NULL)) AS ", printer)))'))->get();

This is returning the result above:

    $json = $listQuery[0];

Then going to use it in this query

    $data = PrintJob::select(DB::raw('DATE(calendar.datefield) as date'), DB::raw('SUM(print_jobs.quantity) as sum'), DB::raw($listQuery))
                ->leftJoin('mo_numbers', 'mo_numbers.mo_id', '=', 'print_jobs.mo_id')
                ->rightJoin('calendar', 'calendar.datefield', '=', 'print_jobs.job_date')
                ->whereDate('calendar.datefield', '>=', date($from))
                ->whereDate('calendar.datefield', '<=', date($to))
                ->groupBy('date')
                ->toSql();
                //->get();
  • 写回答

1条回答 默认 最新

  • dongyi5817 2017-02-21 19:52
    关注

    That's some weird stuff to get, however this will get the value:

    $value = current(json_decode($json, true));
    

    To get the key:

    $key = key(json_decode($json, true));
    

    Or in one swoop:

    list($key, $val) = each(json_decode($json, true));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan