在Ajax中解析Json,错误

Code :

$.ajax({
        type: "POST",
        url: "API URL",
        data: JSON.stringify(User),
        dataType : "json",
        success: function(apiResponse) {
            var session = apiResponse.sessionId;
            console.log ("Session : "+ session);

            $.ajax({
                type: "GET",
                url: "ANOTHER URL",
                dataType : "jsonp",
                contentType: "jsonp",
                success: function(apiResponse) {
                    console.log (apiResponse);
                    jQuery.each(apiResponse,function(){
                         console.log (apiResponse);
                                            });
                     },
                error: function(apiResponse) {
                    alert("error  : " +apiResponse);
                }
            });
        },
        error: function(apiResponse) {
            alert("error  : " +apiResponse);
        }

===========

php code that return json data

<?php

$jsonp = false;
if ( isset( $_GET[ 'callback' ] ) ) {
    $_GET[ 'callback' ] = strip_tags( $_GET[ 'callback' ] );
    $jsonp              = true;
    $pre  = $_GET[ 'callback' ] . '(';
    $post = ');';
  } //isset( $_GET[ 'callback' ] )

 /* Encode JSON, and if jsonp is true, then ouput with the callback
 ** function; if not - just output JSON. */
 $json = json_encode( '{"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]}' );
print( ( $jsonp ) ? $pre . $json . $post : $json );

ANOTHER URL returns following data

  {"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]}

================ Now, I get following error (also mentioning console.log resp)

   Session : 67a47816-5a03-44f9-ab24-01e1e8d4aad1

  {"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]}

   TypeError: invalid 'in' operand e
   [Break On This Error]    

   ...ute(i),"string"==typeof r){try{r="true"===r?!0:"false"===r?!1:"null"===r?null:+r...

=======================

What I want 1. Parse the Json response. "Top Cat1" goes to list heading listings under it.

What I am doing wrong.

Why are you using the function json_encode in a string that is already json encoded?

$json = json_encode( '{"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]}' );

normally you should use:

$json = '"top cat1":[{"id":"cat1", "name":"product1"}, {"id":"cat2", "name":"product 2"}], "top cat2":[{"id":"cat3", "name":"product 3"}, {"id":"cat4", "name":"product 4"}]}';

json_encode is should be used if you had something like that:

$arrayTemp = array("top cat1"=>array(array("id"=>"cat1","name"=>"product1"),array("id"=>"cat2","name"=>"product 2")),"top cat2"=>array(array("id"=>"cat3","name"=>"product 3"),array("id"=>"cat4","name"=>"product 4")));

$json = json_encode($arrayTemp);