Difference between revisions of "LlJsonGetValue"

From Second Life Wiki
Jump to navigation Jump to search
m (true dat)
(Correct examples. JSON entries count from zero, not 1)
Line 11: Line 11:
<lsl>
<lsl>
JGetValTest(){
JGetValTest(){
string j="[[1,2],[4,5,6]]";            //JSON may be written directly as a string like this in sl.
    string j="[[1,2],[4,5,6]]";            //JSON may be written directly as a string like this in sl.
string k;                              //this will change with each command below;
    string k;                              //this will change with each command below;
k=llJsonGetValue(j,[]);                //returns the whole "list" of a JSON. It might just be one entry or a whole nested list or whole nested object.
    k=llJsonGetValue(j,[]);                //returns the whole "list" of a JSON. It might just be one entry or a whole nested list or whole nested object.
//if "j" is a single JSON_STRING, this may return what the string represents as a JSON within a string; a JSON_NUMBER , JSON_TRUE, TRUE ...
    //if "j" is a single JSON_STRING, this may return what the string represents as a JSON within a string; a JSON_NUMBER , JSON_TRUE, TRUE ...
k=llJsonGetValue("\"3.14\"",[]);      //==k="3,14" //float that was stored in a JSON_STRING within a JSON. and not as JSON_NUMBER for no good reason
 
k=llJsonGetValue("\"TRUE\""    ,[]);  //==k="TRUE"            which may be important to communicate with a java application
    k=llJsonGetValue("\"3.14\"",[]);      //==k="3,14" //float that was stored in a JSON_STRING within a JSON. and not as JSON_NUMBER for no good reason
k=llJsonGetValue("\"JSON_TRUE\"",[]);  //==k=JSON_TRUE="�"; //which is the reverse part for an sl scripts "true" states
    k=llJsonGetValue("\"TRUE\""    ,[]);  //==k="TRUE"            which may be important to communicate with a java application
k=               llJsonGetValue(j,[1]);//returns only the first entry. An entry can be many things, a string, a float stored as string,  
    k=llJsonGetValue("\"JSON_TRUE\"",[]);  //==k=JSON_TRUE="�"; //which is the reverse part for an sl scripts "true" states
                                      //a [list] or {object}, each entry being separated by a comma from other entries.
    k=llJsonGetValue(j,[0]);               //returns only the first entry (at offset 0). An entry can be many things, a string, a float stored as string,  
                                      //list and object entries may contain multiple comma separated entries within them.
                                          //a [list] or {object}, each entry being separated by a comma from other entries.
k=               llJsonGetValue(j,[2]);//returns only the second entry... (all the above still counts) k="[4,5,6]";
                                          //list and object entries may contain multiple comma separated entries within them.
k=llJsonGetValue(llJsonGetValue(j,[2]),[3]);
    k=llJsonGetValue(j,[1]);//returns only the second entry... (all the above still applies) k="[4,5,6]";
                                      //instead of getting an entry from "j" we get a sub-entry from llJsonGetValue(j,[2]),  
    k=llJsonGetValue(llJsonGetValue(j,[1]),[2]);
                                      //assuming the sub-entry is a JSON_LIST. It returns the 3rd sub-entry of the second entry,  
                                          //instead of getting an entry from "j" we get a sub-entry from llJsonGetValue(j,[1]),  
                                      //that is a list with 3 entries. it would make jk="6".  
                                          //assuming the sub-entry is a JSON_LIST. It returns the 3rd sub-entry of the second entry,  
                                      //it will return JSON_INVALID if there is no 3rd entry in no 2nd sub-list.
                                          //that is a list with 3 entries. it would make k="6".  
                                          //it will return JSON_INVALID if there is no 3rd entry in no 2nd sub-list.
}
}
</lsl>
</lsl>

Revision as of 08:24, 11 September 2014

Summary

Function: string llJsonGetValue( string json, list specifiers );

Gets the value indicated by specifiers from the json string.
Returns a string made by parsing json, a string representing json and traversing as specified by specifiers.

• string json
• list specifiers

When the input is invalid or no result can be found this function returns JSON_INVALID. If the result is null the function returns JSON_NULL.

Specification

Examples

<lsl> JGetValTest(){

   string j="[[1,2],[4,5,6]]";            //JSON may be written directly as a string like this in sl.
   string k;                              //this will change with each command below;
   k=llJsonGetValue(j,[]);                //returns the whole "list" of a JSON. It might just be one entry or a whole nested list or whole nested object.
   //if "j" is a single JSON_STRING, this may return what the string represents as a JSON within a string; a JSON_NUMBER , JSON_TRUE, TRUE ...
   k=llJsonGetValue("\"3.14\"",[]);       //==k="3,14" //float that was stored in a JSON_STRING within a JSON. and not as JSON_NUMBER for no good reason
   k=llJsonGetValue("\"TRUE\""     ,[]);  //==k="TRUE"            which may be important to communicate with a java application
   k=llJsonGetValue("\"JSON_TRUE\"",[]);  //==k=JSON_TRUE="�"; //which is the reverse part for an sl scripts "true" states
   k=llJsonGetValue(j,[0]);               //returns only the first entry (at offset 0). An entry can be many things, a string, a float stored as string, 
                                          //a [list] or {object}, each entry being separated by a comma from other entries.
                                          //list and object entries may contain multiple comma separated entries within them.
   k=llJsonGetValue(j,[1]);//returns only the second entry... (all the above still applies) k="[4,5,6]";
   k=llJsonGetValue(llJsonGetValue(j,[1]),[2]);
                                          //instead of getting an entry from "j" we get a sub-entry from llJsonGetValue(j,[1]), 
                                          //assuming the sub-entry is a JSON_LIST. It returns the 3rd sub-entry of the second entry, 
                                          //that is a list with 3 entries. it would make k="6". 
                                          //it will return JSON_INVALID if there is no 3rd entry in no 2nd sub-list.

} </lsl>

see LlJsonValueType for LlJsonGetValue()-examples, because its better to test if the LlJsonValueType() is correct before getting its value as that type.

See Also

Functions

•  llList2Json
•  llJson2List
•  llJsonSetValue
•  llJsonValueType

Articles

•  Typecast

Deep Notes

History

Date of Release 20/05/2013

All Issues

~ Search JIRA for related Issues
   JSON_NULL may be deceptively returned instead of JSON_INVALID when noncompliant Json text is encountered by either llJsonValueType or llJsonGetValue. Fixed with release 13.09.21.281328.

Signature

function string llJsonGetValue( string json, list specifiers );