dongmou5628 2010-09-05 04:42
浏览 58
已采纳

定位json数组的特定部分,如果存在则创建表

I have a complex json array which I want to target a specific line/string and if present then add a small table to web page...

This is the array

{
"coastalWarnings":[{
  "loc":"ABEL",
  "warn":"GALE"
  },{
  "loc":"CASTLEPOINT",
  "warn":"STORM"
  },{
  "loc":"CHALMERS",
  "warn":"GALE"
  },{
  "loc":"CHATHAM ISLANDS",
  "warn":"GALE"
  },{
  "loc":"CONWAY",
  "warn":"STORM"
  },{
  "loc":"COOK",
  "warn":"STORM"
  },{
  "loc":"FOVEAUX",
  "warn":"GALE"
  },{
  "loc":"GREY",
  "warn":"GALE"
  },{
  "loc":"MILFORD",
  "warn":"STORM"
  },{
  "loc":"PORTLAND",
  "warn":"GALE"
  },{
  "loc":"PUYSEGUR",
  "warn":"STORM"
  },{
  "loc":"RAGLAN",
  "warn":"GALE"
  },{
  "loc":"RANGITATA",
  "warn":"GALE"
  },{
  "loc":"STEPHENS",
  "warn":"GALE"
  }
  ],
"isAdvisory":true,
"isOutlook":true,
"isVhf":true,
"isWatch":true,
"liftedWarnings":[
  ],
"oceanicWarnings":[{
  "loc":"FORTIES",
  "warn":"GALE"
  },{
  "loc":"PACIFIC",
  "warn":"GALE"
  },{
  "iceAccretion":true,
  "loc":"SOUTHERN",
  "warn":"GALE"
  },{
  "loc":"SUBTROPIC",
  "warn":"GALE"
  }
  ],
"roadSnowWarnings":[
  ],
"severeWarnings":[{
  "loc":"BULLER",
  "warn":["ISSUE"
    ]
  },{
  "loc":"CANTERBURY",
  "warn":["ISSUE"
    ]
  },{
  "loc":"FIORDLAND",
  "warn":["ISSUE"
    ]
  },{
  "loc":"HAWKES BAY",
  "warn":["ISSUE"
    ]
  },{
  "loc":"MANAWATU",
  "warn":["ISSUE"
    ]
  },{
  "loc":"MARLBOROUGH",
  "warn":["ISSUE"
    ]
  },{
  "loc":"NELSON",
  "warn":["ISSUE"
    ]
  },{
  "loc":"OTAGO",
  "warn":["ISSUE"
    ]
  },{
  "loc":"SOUTHLAND",
  "warn":["ISSUE"
    ]
  },{
  "loc":"TAIHAPE",
  "warn":["ISSUE"
    ]
  },{
  "loc":"TARANAKI",
  "warn":["ISSUE"
    ]
  },{
  "loc":"TAUMARUNUI",
  "warn":["ISSUE"
    ]
  },{
  "loc":"TAUPO",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WAIRARAPA",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WAITOMO",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WANGANUI",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WELLINGTON",
  "warn":["ISSUE"
    ]
  },{
  "loc":"WESTLAND",
  "warn":["ISSUE"
    ]
  }
  ],
"thunderstormWarnings":[
  ]
}

As you can see the array is broken down into parts -

"coastalWarnings"
"liftedWarnings"
"oceanicWarnings"
"roadSnowWarnings"
"severeWarnings"
"thunderstormWarnings"

I wish to target "severeWarnings" and in particular "loc":"CANTERBURY" which displays as -

  },{
  "loc":"CANTERBURY",
  "warn":["ISSUE"
    ]
  },{

so when present in array it will trigger a table such as

<table align="center" width="100%" bgcolor="red">
     <tr><td><strong>Severe Weather Warning in effect for CANTERBURY</strong></td></tr>
</table>

The biggest problem is the array changes all the time so need to target I guess by searching for specific words or wording unless it can be broken down into parts reliably.

Could something as simple as this work?

<?
$table = <table align="center" width="100%" bgcolor="red"><tr><td><strong>Severe Weather Warning in effect for CANTERBURY</strong></td></tr></table>;

$file = file_get_contents("filename.ext");
if(!strpos($file, ""loc":"CANTERBURY"")) {
echo $table;
}
?

Unsure how to go about this and my php skills are fairly limited so if you would be so kind as to show me a script or a step by step on how to achieve this, I would be ever so appreciative.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongzhunqiu4841 2010-09-05 07:41
    关注

    As Alex suggested, use json_decode

    $s = getData(); // returning the string from the question
    $data = json_decode($s, true);
    
    foreach( $data['severeWarnings'] as $cw ) {
      if ( 'CANTERBURY'===$cw['loc'] ) {
        foreach( $cw['warn'] as $msg ) {
          echo "-- ", $msg, " --
    ";
        }
      }
    }
    

    prints -- ISSUE --

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导