I want to parse the raw JSON text format (which is a rich text format used by Minecraft) into HTML, so I can fetch the data from a Minecraft server then put it on a website. But, searches return no relevant information (aside from the legacy color codes, which are way simpler). I have no idea how to do it. Is there any library that might make it simpler or any hints on this? Here's an example:
{"extra":[{"color":"light_purple","text":"... "},{"color":"green","text":"hello world"}],"text":""}
CodePudding user response:
So for the instance that you have given, this code will probably work:
<?php
$jsonString = '{"extra":[{"color":"light_purple","text":"... "},{"color":"green","text":"hello world"}],"text":""}';
$obj = json_decode($jsonString);
foreach($obj->extra as $item){
echo "<span class='".$item->color."'>".$item->text."</span>";
}
?>
What I would recommend is including a .css file as well containing the colors inside of classes like so:
.light_purple {
color:#FF55FF;
}
.green {
color:#55FF55;
}
I actually found a good resource containing the actual hex codes for those minecraft colors here.. There really aren't that many colors so it shouldn't be too bad to add onto your project!