Jump to content

How can i get Json value !!!


Go to solution Solved by Barand,

Recommended Posts

{
  "parentsGuide": [
    {
      "category": "FRIGHTENING_INTENSE_SCENES",
      "severityBreakdowns": [
        {
          "severityLevel": "none",
          "voteCount": 40
        },
        {
          "severityLevel": "mild",
          "voteCount": 135
        },
        {
          "severityLevel": "moderate",
          "voteCount": 41
        },
        {
          "severityLevel": "severe",
          "voteCount": 22
        }
      ],
      "reviews": [
        {
          "text": "Much darker and mature than the first movie."
        },
        {
          "text": "The whole plot of the movie is a bit cruel and dark.",
          "isSpoiler": true
        },
        {
          "text": "The 'Kristen Bell' song, \"The Next Right Thing\" , may be dark and mature because it's about losing someone you love, but it also means stepping up for them.",
          "isSpoiler": true
        },
        {
          "text": "The Earth Giants (aka Jötuun) may scare younger kids, such as they roar very loudly and throw boulders at the main/minor characters.",
          "isSpoiler": true
        },
        {
          "text": "Anna nearly becomes infected to the Fire Spirit, but is saved by Kristoff and Elsa.",
          "isSpoiler": true
        },
        {
          "text": "A character thaws and falls into the water."
        },
        {
          "text": "Despite the fact that it's a kids movie, this film is a lot more darker than the original."
        }
      ]
    },
    {
      "category": "SEXUAL_CONTENT",
      "severityBreakdowns": [
        {
          "severityLevel": "none",
          "voteCount": 215
        },
        {
          "severityLevel": "mild",
          "voteCount": 25
        },
        {
          "severityLevel": "moderate",
          "voteCount": 5
        },
        {
          "severityLevel": "severe",
          "voteCount": 30
        }
      ],
      "reviews": [
        {
          "text": "Sven looks at his reflection in a shop window, where a mannequin is modelling a dress and the position of his reflection makes it look like he is wearing the dress."
        },
        {
          "text": "Anna accidentally pulls up a clothes line instead of a line of flags, resulting in a pair of undergarments on the line being positioned in front of her where they would be worn."
        },
        {
          "text": "Kristoff attempts to propose to Anna on several occasions, but keeps messing up. Eventually he succeeds.",
          "isSpoiler": true
        }
      ]
    },
    {
      "category": "VIOLENCE",
      "severityBreakdowns": [
        {
          "severityLevel": "none",
          "voteCount": 74
        },
        {
          "severityLevel": "mild",
          "voteCount": 135
        },
        {
          "severityLevel": "moderate",
          "voteCount": 17
        },
        {
          "severityLevel": "severe",
          "voteCount": 18
        }
      ]
    },
    {
      "category": "PROFANITY",
      "severityBreakdowns": [
        {
          "severityLevel": "none",
          "voteCount": 194
        },
        {
          "severityLevel": "mild",
          "voteCount": 10
        },
        {
          "severityLevel": "moderate",
          "voteCount": 4
        },
        {
          "severityLevel": "severe",
          "voteCount": 24
        }
      ]
    },
    {
      "category": "ALCOHOL_DRUGS",
      "severityBreakdowns": [
        {
          "severityLevel": "none",
          "voteCount": 197
        },
        {
          "severityLevel": "mild",
          "voteCount": 2
        },
        {
          "severityLevel": "moderate",
          "voteCount": 1
        },
        {
          "severityLevel": "severe",
          "voteCount": 18
        }
      ]
    }
  ]
}

here it is 

the problem is the word that after category

  • Solution

Some of your text values contain single quotes which must be escaped (\')

parentsGuide is an array of category objects
each category object contains a severityBreakdowns array of severity objects and may contain a reviews array of review objects

Try

$data = json_decode($json_string);

echo '<pre>' . print_r($data, 1) . '</pre>';          // print_r output easier to rea than var_dump

##
##  output the data
##
foreach ($data->parentsGuide as $cat)   {
    
    echo "<h3>$cat->category</h3>";
    foreach ($cat->severityBreakdowns as $sev)  {
        printf("&bull; %s (%d)<br>", $sev->severityLevel, $sev->voteCount);
    }
    if (isset($cat->reviews))  {
        echo "<h4>Reviewa</h4>";
        foreach ($cat->reviews as $review)  {
            echo "<i>$review->text</i><br>";
        }
    }
}

... giving ...

image.png.d4e9a801d5e82b51b7ba3a802c01e2fb.png

7 hours ago, 7cc said:

to be more clearly i want to work on ... category: SEXUAL_CONTENT

you need to take this into account when you operate on the data, and when you ask questions in a programming help forum. for the same reason you would have an index for data in a database, you would index the data in an array, using a key that you want to directly reference the data by. in this case the category.

$data = json_decode($json_string,true);

// index the data using the category as the key
$result = [];
foreach($data['parentsGuide'] as $arr)
{
	$result[$arr['category']] = $arr;
}

// directly reference the data that you want - $result["SEXUAL_CONTENT"]
echo '<pre>'; print_r($result["SEXUAL_CONTENT"]); echo '</pre>';

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.