处理JSONArray,JSONObject

时间:2016-04-19 17:50:22

标签: arrays json object processing

我有以下代码:

 
<?php
    // You may use status(), start(), and stop(). notice that start() method gets called automatically one time.
    $process = new Process('ls -al');

    // or if you got the pid, however here only the status() metod will work.
    $process = new Process();
    $process.setPid(my_pid);
?>

<?php
    // Then you can start/stop/ check status of the job.
    $process.stop();
    $process.start();
    if ($process.status()){
        echo "The process is currently running";
    }else{
        echo "The process is not running.";
    }
?>

<?php
/* An easy way to keep in track of external processes.
* Ever wanted to execute a process in php, but you still wanted to have somewhat controll of the process ? Well.. This is a way of doing it.
* @compability: Linux only. (Windows does not work).
* @author: Peec
*/
class Process{
    private $pid;
    private $command;

    public function __construct($cl=false){
        if ($cl != false){
            $this->command = $cl;
            $this->runCom();
        }
    }
    private function runCom(){
        $command = 'nohup '.$this->command.' > /dev/null 2>&1 & echo $!';
        exec($command ,$op);
        $this->pid = (int)$op[0];
    }

    public function setPid($pid){
        $this->pid = $pid;
    }

    public function getPid(){
        return $this->pid;
    }

    public function status(){
        $command = 'ps -p '.$this->pid;
        exec($command,$op);
        if (!isset($op[1]))return false;
        else return true;
    }

    public function start(){
        if ($this->command != '')$this->runCom();
        else return true;
    }

    public function stop(){
        $command = 'kill '.$this->pid;
        exec($command);
        if ($this->status() == false)return true;
        else return false;
    }
}
?>

我的Json文件:

JSONObject json;
int max;
void setup() {
  size(500,500);

JSONObject json = loadJSONObject("january_2016.json");
JSONObject maxTemperature = json.getJSONObject("Max Temperature");
int max = maxTemperature.getInt("max");

  print(max);
}

void draw(){

  ellipse(max, 10, 100, 100);
}

现在运行代码时出现以下错误:
[ { "Max Temperature": { "max": "18", "avg": "6", "min": "-2" } ]

我明白因为[]我需要使用JSONArray但是如果我将JSONObject must begin with {
更改为JSONArray,我会收到此错误:
JSONObject json = loadJSONObject("january_2016.json");<br>

我相信它一定很简单,但我对此很新,所以提前谢谢

1 个答案:

答案 0 :(得分:1)

我认为你误解了一些关于JSON和Processing如何工作的基础知识。

让我们看看一些JSON示例。 JSONObject是由大括号{}包围的一组键/值对。这是JSONObject

{
   "max": "18", 
   "avg": "6", 
   "min": "-2"
}

如果您有JSONObject,则可以按字符串键获取值。

您还可以嵌套JSONObjects,以便JSONObject包含其值本身为JSONObject的键。像这样:

{
   "Max Temperature":  {
      "max": "18", 
      "avg": "6", 
      "min": "-2"
   }
}

您还可以拥有JSONArray而不是JSONObjectJSONArray是由方括号[]包围的一组值。这是JSONArray

 [
   {
     "id": 0,
     "species": "Capra hircus",
     "name": "Goat"
   },
   {
     "id": 1,
     "species": "Panthera pardus",
     "name": "Leopard"
   },
   {
     "id": 2,
     "species": "Equus zebra",
     "name": "Zebra"
   }
 ]

此示例JSONArray取自the reference,其中包含3个JSONObjects

如果您有JSONArray,那么您必须从特定索引中获取值。

现在让我们来看看你的JSON:

[
  {
    "Max Temperature": {
      "max": "18", 
      "avg": "6", 
      "min": "-2"
    }
]

您的JSON以方括号[开头,这意味着它是JSONArray

JSONArray jsonArray = loadJSONArray("january_2016.json");

好的,现在你有了JSONArray这意味着您必须通过特定的int索引访问数据。在您的情况下,您只有一个索引,因此它的索引0

JSONObject jsonObject = jsonArray.getJSONObject(0);

jsonObject变量现在包含此JSON:

{
"Max Temperature": {
  "max": "18", 
  "avg": "6", 
  "min": "-2"
}

从这里开始,您应该可以使用我在其他问题中向您展示的功能来解析JSONObject

您也可以修改JSON文件,以便直接将其用作JSONObject