-1

This is my JSON.

{
  "OUT_STAT": "200",
  "OUT_MESS": "SUKSES",
  "OUT_DATA": [{
      "id_doc_proj": "4"
    }]
}

My Questions are:
1. How to get OUT_STAT value?
2. How to get id_doc_proj value?

I am sorry if my questions are silly because i am new at get value from JSON.

Thanks in advance.

UPDATE

I am sorry if you are saying i duplicated Parsing JSON file with PHP. I have tried code from there but i don't get any JSON from my response. I don't know where is the mistake. If you want to help, this is my php script.

<?php
    $file_path = "";
    $id_project = "16";
    $p_id_doc_proj = "50";
    $id_doc_type = "1";
    $id_user = "4";

    $url = $file_path.basename($_FILES['uploaded_file']['name']);

    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $url)){
        $ws = "http://172.xx.x.xx:xxxx/rest/com/acc/uw/in/httprest/apponline/uploadtrough/UploadImage/$id_project/$p_id_doc_proj/$id_doc_type/$id_user/$url";

        $opts = array('http'=>array('header'=>'Content-type: application/x-www-form-urlencoded'));

        $context = stream_context_create($opts);

        $arrayLog=array();
        $data1 = file_get_contents($ws, false, $context);
        $result = json_decode($data1, true);;

        //$result = array("result" => "success");
    }else{
        $result = array("result" => "error");
    }

    echo json_encode($result);
?>

The result should show my JSON above but it was null. Any answers will help me. Thanks in advance.

Marcel Ruben
  • 81
  • 12
  • use json_decode() and then get the value by array – lazyCoder Jun 06 '17 at 04:59
  • 1
    Possible duplicate of [Parsing JSON file with PHP](https://stackoverflow.com/q/4343596/6521116) – LF00 Jun 06 '17 at 04:59
  • $a='{ "OUT_STAT": "200", "OUT_MESS": "SUKSES", "OUT_DATA": [{ "id_doc_proj": "4" }] }'; $a=json_decode($a); echo $out_stat = $a->OUT_STAT $out_data =$a->OUT_DATA; foreach ($out_data as $key => $value) { echo $id_doc_proj = $value->id_doc_proj; # code... } – Hussy Borad Jun 06 '17 at 05:06
  • Sorry if i duplicated, i have updated my question @KrisRoofe – Marcel Ruben Jun 06 '17 at 07:23

2 Answers2

1
$data="{
  "OUT_STAT": "200",
  "OUT_MESS": "SUKSES",
  "OUT_DATA": [{
      "id_doc_proj": "4"
    }]
}";

$op=json_decode($data, true);

echo $op['OUT_STAT'];
echo $op['OUT_DATA'][0]['id_doc_proj'];

http://php.net/json_deocde. it will make output as associative array. you can then use the result as you would do with an assoc array. you can also use var_dump() to introspect the structure.

varuog
  • 3,031
  • 5
  • 28
  • 56
-1

you can use json_decode to convert json to array or object:

$json = '{
  "OUT_STAT": "200",
  "OUT_MESS": "SUKSES",
  "OUT_DATA": [{
      "id_doc_proj": "4"
    }]
}';

$object = json_decode($json);
$out_stat = $object->OUT_STAT;
$id_doc_proj = $object->OUT_DATA[0]->id_doc_proj;
Mohammad Hamedani
  • 3,304
  • 3
  • 10
  • 22
  • you are like no matter whether user say it is duplicate question, i ll give the simple answer to increase my repo – lazyCoder Jun 06 '17 at 05:07