How to use a string from the database [closed]

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 did not figure out how to use it, like for example, to get the week201549-> information. I've tried json_decode and a lot of things and not anything.

I do not know what to do, or would I have some other way to save it in the database?

    
asked by anonymous 10.12.2015 / 18:58

3 answers

1

I solved the problem.

At the time of saving to the database I put a mysqli_real_escape_string and it worked everything else.

Putz, and me breaking my head thinking it was json's problem.

    
10.12.2015 / 20:50
0

You can use json_decode() as a response to @Raylan, following 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 shortened your% of% because it was too large and incomplete

    
10.12.2015 / 19:43
-2

I think 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 to use something like the java split function (or some similar depending on the language you are using), it breaks the string using a character you pass by parameter and throws it in a vector. / p>

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'

    
10.12.2015 / 19:13