{\"primary\":{\"type\":\"editable\",\"value\":\"FIRST\",\"snippets\":{}},\"secondary\":{\"type\":\"editable\",\"value\":\"SECOND\",\"snippets\":{}}}

I think that I need to use JSON decode to get the data out of this...
but I am not really sure how to format it...

could anyone show me a code example of how to get out just the values FIRST and SECOND,

thanks!

    What's with all of the backslashes? Where are you getting this data, and why is that source giving you malformed data?

      Well I'm guessing it came thru a POST or GET based on this same string in another of his threads. My guess is that the (Not-Very-)Magic-Quotes is on. Therefore given the string:

      <?php
      
      $str = '{\"primary\":{\"type\":\"editable\",\"value\":\"FIRST\",\"snippets\":{}},\"secondary\":{\"type\":\"editable\",\"value\":\"SECOND\",\"snippets\":{}}}';
      $str = stripslashes($str); // Only necessary because the data you provided appears to have been run to either addslashes, or magic_quotes. Disable magic_quotes and this line can be removed.
      
      $json = json_decode($str);
      foreach( $json as $k => $v ) {
         echo $k .' '. $v->value .'<br>';
      }
        3 months later

        sweet im getting closer. I just came from another forum which I'll never use again, every question is answered with a question.. and your usual "godlike" member never helping yet just answering questions with questions.

        so.. im here lol.. Like the example above.. how would I decode this one please.

        {"page_number":1,"total_rows":2,"date":"April, 19 2012 00:44:42","stats":[
        
        {"rnk":1,"top":4,"ref":1000007,"3Per":1.0,"sts":4,"w":2,"p":2,"sF":00112,"es":8.0020,"reg":"X","hN":"Defence","s":0,"winP":0.98},
        {"rnk":3,"top":2,"ref":1000001,"3Per":1.0,"sts":4,"w":2,"p":2,"sF":00112,"es":8.0020,"reg":"X","hN":"Defence","s":0,"winP":0.98},
        {"rnk":2,"top":4,"ref":1000000,"3Per":1.0,"sts":4,"w":2,"p":2,"sF":00111,"es":7.0002,"reg":"X","hN":"Forward","s":9,"winP":0.12}
        
        ]}
        
        
        
        
        

        Thanks so much in advance!!!
        I spent 6 hours being messed around my an administrator on the other forum.. i dont even know if he had an actual answer for me.

          0o0o0;11002584 wrote:

          so.. im here lol.. Like the example above.. how would I decode this one please.

          same as in the example above; using [man]json_decode/man.

          0o0o0;11002584 wrote:

          I just came from another forum which I'll never use again, every question is answered with a question.. and your usual "godlike" member never helping yet just answering questions with questions.

          I don't know, of course, but answering coding questions with other questions is usually not arrogance or ridicule: more often, it's an attempt to get a person to think more critically about what they're asking, and give insights into the question so the person can solve the problem themselves. Typically, there are two main reasons for this approach:

          1) guiding someone along the problem-solving process is far more beneficial than simply giving a "cut-and-paste" answer. It helps the person improve their skills, and not get caught by similar problems in the future.

          2) offering assistance to someone who is trying to solve a problem for themselves is far more satisfying than just "fixing" code for them. While this may seem like a selfish motivation, keep in mind that most people who offer help on forums, like this one, are volunteering their time and have no obligation to offer help at all.

            Assuming it is valid JSON (which you can check here), json_decode() should work just fine with it. You may want to set its optional 2nd parameter to true so that it returns everything as arrays (no objects). For a quick view of how it parsed, you can then use print_r(), e.g.:

            $dataArray = json_decode($inputJsonString, true);
            echo "<pre>".print_r($dataArray, true)."</pre>";
            

              What traq said. Another reason why a question might be answered by a question is simply to get clarification of what the problem is - sometimes a bit of background to put the problem in context might be needed.

                NogDog;11002599 wrote:

                Assuming it is valid JSON (which you can check here)...

                tangent: is there a way in PHP to check if a string is valid JSON (other than trying to decode it and then checking for an error)?

                  im really new at json here.. so out of all the examples ive read on the net (32 places) .. they all give strings as examples..
                  ive tried them all they work.. all great.. but call me slow or stupid..
                  the question is how do you make it grab a file txt, etc.. and make it grab the entire file and parce it?? or is json on the fly only??

                  $dataArray = json_decode($inputJsonString, true); 
                  echo "<pre>".print_r($dataArray, true)."</pre>"; 
                  

                  Ya I get this but I really dont lol..

                  i want all the rnk off a page all the top all the ref etc etc..

                  in every example on the net it shows the entire first line.. {"rnk":1,"top":4,"ref":1000007,"3Per":1.0,"sts":4 etc etc...

                  wouldnt it just be "rnk", "top", "ref".. like sql ?? or no confused.. big time here lol

                  {"page_number":1,"total_rows":2,"date":"April, 19 2012 00:44:42","stats":[
                  
                  {"rnk":1,"top":4,"ref":1000007,"3Per":1.0,"sts":4,"w":2,"p":2,"sF":00112,"es":8.0020,"reg":"X","hN":"Defence","s":0,"winP":0.98},
                  {"rnk":3,"top":2,"ref":1000001,"3Per":1.0,"sts":4,"w":2,"p":2,"sF":00112,"es":8.0020,"reg":"X","hN":"Defence","s":0,"winP":0.98},
                  {"rnk":2,"top":4,"ref":1000000,"3Per":1.0,"sts":4,"w":2,"p":2,"sF":00111,"es":7.0002,"reg":"X","hN":"Forward","s":9,"winP":0.12}
                  
                  ]}
                  
                    bradgrafelman;11002620 wrote:

                    Why exclude doing that?

                    1) that's the method I already know about.

                    2) it would seem logical that there would be a way to find out without creating an object/array -- i.e., a boolean return value.

                    0o0o0;11002621 wrote:

                    im really new at json here.. so out of all the examples ive read on the net (32 places) .. they all give strings as examples..
                    ive tried them all they work.. all great.. but call me slow or stupid..
                    the question is how do you make it grab a file txt, etc.. and make it grab the entire file and parce it?? or is json on the fly only??

                    the reason that all the json examples you've seen use strings is because json uses a string format. The strings can come from any number of places, such as your own script, ajax requests, or text files:

                    $json_string = file_get_contents( '/path/to/jsonfile.txt' );
                    $json_object = json_decode( $json_string );
                    switch (json_last_error()) {
                        case JSON_ERROR_NONE:
                            print '<pre>';
                            var_dump( $json_object );
                            print '</pre>';
                        break;
                        case JSON_ERROR_DEPTH:
                            echo ' - Maximum stack depth exceeded';
                        break;
                        case JSON_ERROR_STATE_MISMATCH:
                            echo ' - Underflow or the modes mismatch';
                        break;
                        case JSON_ERROR_CTRL_CHAR:
                            echo ' - Unexpected control character found';
                        break;
                        case JSON_ERROR_SYNTAX:
                            echo ' - Syntax error, malformed JSON';
                        break;
                        case JSON_ERROR_UTF8:
                            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
                        break;
                        default:
                            echo ' - Unknown error';
                        break;
                    }
                    0o0o0;11002621 wrote:

                    Ya I get this but I really dont lol..

                    i want all the rnk off a page all the top all the ref etc etc..

                    in every example on the net it shows the entire first line.. {"rnk":1,"top":4,"ref":1000007,"3Per":1.0,"sts":4 etc etc...

                    wouldnt it just be "rnk", "top", "ref".. like sql ?? or no confused.. big time here lol

                    I am not sure what you're trying to ask, here.

                    no, it would not be like SQL. JSON is not related to SQL.

                    to get the "rnk" value from each object, you might do something like:

                    // parse the JSON as an assoc. array
                    $json_array = json_decode( $yourJSONstring,TRUE );
                    
                    // loop though the array, checking for 'rnk' values
                    foreach( $json_array['stats'] as $array ){
                        $rnk[] = empty( $array['rnk'] )? 'rnk missing': $array['rnk'];
                    }
                    
                    print '<pre>';
                    var_dump( $rnk );
                    print '</pre>';

                      lol after all this json decode may not be what Im looking for..

                      I get this output..
                      for rnk...
                      array(100) {
                      [0]=>
                      int(1)
                      [1]=>
                      int(2)
                      [2]=>
                      int(3)
                      [3]=>
                      int(4)

                      for the next
                      array(100) {
                      [0]=>
                      int(1)
                      [1]=>
                      int(2)
                      [2]=>
                      int(3)
                      [3]=>
                      int(4)

                      but what im actually trying to do is.. make it into a csv format? like
                      1,1,etc, etc,etc
                      2,2,etc,etc,etc
                      3,7,73,28,28
                      someone mentioned json so I went from there.. and yes I went to that verification page and it said Valid Json.

                      Heres what im trying to do... I get the json file/txt... I then want to rip/parse it in php... then send it to a database..

                      ...and once the scripts made and works.. im making a cron out of it 2 times a day.

                      The format part comma delimited is this even possible?

                      cause it outputs
                      array(100) {
                      [0]=>
                      int(1)
                      [1]=>
                      int(2)
                      [2]=>
                      int(3)
                      [3]=>
                      int(4)

                      then the next field is below this..

                      thanks my last question before completely submerging myself into online documentation. I admit Im deliberately not getting into the deep end of json, but merely asking for some code hints.

                        it's actually not too difficult to get to CSV from this point - but do you want CSVs, or do you want to insert it into a DB? Don't put CSVs into a database.

                        <?php
                        
                        $json_string = '{"page_number":"1","total_rows":"2","date":"April, 19 2012 00:44:42","stats":[{"rnk":"1","top":"4","ref":"1000007","3Per":"1.0","sts":"4","w":"2","p":"2","sF":"00112","es":"8.0020","reg":"X","hN":"Defence","s":"0","winP":"0.98"},{"rnk":"3","top":"2","ref":"1000001","3Per":"1.0","sts":"4","w":"2","p":"2","sF":"00112","es":"8.0020","reg":"X","hN":"Defence","s":"0","winP":"0.98"},{"rnk":"2","top":"4","ref":"1000000","3Per":"1.0","sts":"4","w":"2","p":"2","sF":"00111","es":"7.0002","reg":"X","hN":"Forward","s":"9","winP":"0.12"}]}';
                        
                        // parse into assoc.array
                        $json_array = json_decode( $json_string,TRUE );
                        
                        // loop through "stats"
                        foreach( $json_array['stats'] as $stats ){
                        	// turn each array into a CSV list
                        	$csv[] = implode( ',',$stats );
                        }
                        // print all the CSVs (to see the result)
                        print nl2br( implode( "\n",$csv ) );
                        /* outputs something like:
                        1,4,1000007,1.0,4,2,2,00112,8.0020,X,Defence,0,0.98
                        3,2,1000001,1.0,4,2,2,00112,8.0020,X,Defence,0,0.98
                        2,4,1000000,1.0,4,2,2,00111,7.0002,X,Forward,9,0.12
                        */

                          omg that was beautiful! thanks.

                          ok just for knowledge sake, by your reply it sounded like what I was asking for is overkill, maybe outdated?? just sounded like you know a better method to put it in a database.
                          See I made online realtor software in 05 took a couple months learning php step by step while building it. The local city database only offered csv format.. so I made it explode and parse and in the database it goes.. been running for 7 years straight without a hiccup.. so its all I really know.. when i found this json file, all I was looking for was a task of getting it into a format id be familiar with. To dump to a db. But if theres a better method online, tutorial please lemme know. Seeing that theirs alot fo json out there now a days.

                          Again thanks for the last message you sent tho.

                            well, I might be wrong about your intentions. are you:

                            -- using the CSV to populate a database (e.g., like Excel, where you can import values into the appropriate columns using a CSV)?

                            That's fine, I guess. There's probably a less error-prone way to do it, but that would depend on the DB.

                            -- simply inserting the CSV into a database (i.e., the whole CSV just goes into a column)?

                            That's not okay. It defeats the purpose of storing data in a database. Each column in the DB should only contain one value, not a whole list of them.

                            (if this is what you're doing, you should just cut out all the extra steps and store the JSON string itself in a text file.)

                              hopin.. thi

                              $sql = 'LOAD DATA LOCAL INFILE \'/home/sites/www.soon to be the json to csv.com/sample/data.csv\' INTO TABLE `JSONSOON TO BE EXAMPLE` FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' ESCAPED BY \'\\\\\' LINES TERMINATED BY \'\\r\\n\''; 
                              

                              im sure when I get to this part.. the enclose terminated and lines terminated will have to be looked at closer.

                              lemme try it youve helped out excessively from where I was first coming from.

                              oh wait I know what your saying why csv, why not simple text file???????? am I correct thats what your askin? lol.. ok txt file sure..does it handle it the same?

                                it would depend on your objective.

                                If you want to simply store all of the data for later loading and use, a text file would be fine.

                                If you ever (now or if any possibility exists in the future) need to search the data, or retrieve only parts of it, then you need a database.
                                I've never loaded data to SQL using CSV format, so I couldn't help you with your query.

                                  Write a Reply...