How to use a string from the database

Asked

Viewed 66 times

3

I have the following string saved in the database.

{"key":"save","user":"1","season":"2016","week201549":{"bloco":"Microciclo","day05122015":{"z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""},"day06122015":{"z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""}},"week201550":{"bloco":"Microciclo","day07122015":{"z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""},"day08122015":{"z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""},"day09122015":{"z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""},"day10122015":{"z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""},"day11122015":{"z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""},"day12122015":{"z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""},"day13122015":{"z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""}},"week201551":{"bloco":"Microciclo","day14122015":

I just couldn’t figure out how to use it, I’d like to take the information week201549->block. I’ve tried json_decode and a lot more things and not from nothing.

I don’t know what to do, or would have some other way to save in the database?

  • Hello, if I understand your need I believe this link can help you: http://php.net/manual/en/function.json-decode.php

  • The json placed in the question has a syntax error.

  • And what would that mistake be?

  • Play this string in jsonlint and change the call to: json_decode($str) or die(json_last_error_msg())

  • I put it in jsonlint: json_decode($str) and returned the following Error: Parse error on line 1: json_decode({ "key" Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'Undefined'

  • jsonlint reported a bug?

  • Error: Parse error on line 1: json_decode({ "key" Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'Undefined' Source is on Github. Props to Douglas Crockford of JSON and JS Lint and

Show 2 more comments

3 answers

1


I solved the problem.

At the time of saving in the database I put a mysqli_real_escape_string and worked all the rest.

Putz, and me cracking my head thinking it was json trouble.

0

You can use the json_decode() as per @Raylan’s reply, below is an example:

$stringBanco = '{
    "key":"save",
    "user":"1",
    "season":"2016",
    "week201549": {
        "bloco":"Microciclo",
        "day05122015":{     "z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""
            },
        "day06122015":{ "z1":"0","z2":"0","z3":"0","z4":"0","z5":"0","z6":"0","z7":"0","terrain":"Terreno","rpe":"7","elevation":"1861","fc":"140","time":"250","distance":"86","training":" ","color":""
            }
        }
    }';

$json = json_decode($stringBanco) or die(json_last_error_msg());

echo '<pre>';
    print_r($json->week201549);
echo '</pre>';

Note: I abbreviated your a string because she was too big and incomplete

  • 1

    You’re giving syntax error, I don’t understand why.

  • 1

    Probably the string that is saved in the bank is incorrect

  • Yes, something from the database was going wrong.

-2

I believe that the ideal would be to break this string into entities and attributes within the database, for example "key", "user", "Season" would be attributes of an entity. If this is not possible, you can try using something like the java split function (or something similar depending on the language you are using), it breaks the string using a character you pass by parameter and throws it into a vector.

For example:

String frase = "{'key':'save','user':'1','season':";  
String array[] = new String[3];    
array = frase.split(":");  

array[0] = {'key'

array[1] = 'save','user'

array[2] = '1','Season'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.