7cc Posted March 21 Share Posted March 21 Hi every one I encountered a problem while trying to fetch a variable from a json file $VAR = $jsonData->parentsGuide->category-severityBreakdowns; Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/ Share on other sites More sharing options...
Barand Posted March 21 Share Posted March 21 Post the original json string (as it is in the file) so I can examine it's structure. Then I can see how you need to access its elements. NOTE: post as text and not a useless image. Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/#findComment-1663318 Share on other sites More sharing options...
7cc Posted March 21 Author Share Posted March 21 { "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 Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/#findComment-1663319 Share on other sites More sharing options...
R1fts Posted March 22 Share Posted March 22 category and severityBreakdowns are on the same level just use: $jsonData->parentsGuide->severityBreakdowns; Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/#findComment-1663322 Share on other sites More sharing options...
7cc Posted March 22 Author Share Posted March 22 12 hours ago, R1fts said: category and severityBreakdowns are on the same level just use: $jsonData->parentsGuide->severityBreakdowns; u can't bypass the category because u don't know witch one u working on it, thanks bro Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/#findComment-1663325 Share on other sites More sharing options...
Solution Barand Posted March 22 Solution Share Posted March 22 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("• %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 ... Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/#findComment-1663326 Share on other sites More sharing options...
7cc Posted March 22 Author Share Posted March 22 Unfortunately not success to be more clearly i want to work on category: SEXUAL_CONTENT Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/#findComment-1663329 Share on other sites More sharing options...
maxxd Posted March 22 Share Posted March 22 Barand has given you the answer. You're looping through the array and printing out data. If you don't want to print some of the data, don't. If you want the word 'category:' in front of the category text, put it there in the output statement. Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/#findComment-1663332 Share on other sites More sharing options...
mac_gyver Posted March 23 Share Posted March 23 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/#findComment-1663333 Share on other sites More sharing options...
7cc Posted March 23 Author Share Posted March 23 thanks every one finely i got the resulte Quote Link to comment https://forums.phpfreaks.com/topic/332853-how-can-i-get-json-value/#findComment-1663334 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.