LlJsonSetValue/de

From Second Life Wiki
< LlJsonSetValue
Revision as of 22:45, 21 October 2013 by MartinRJ Fayray (talk | contribs) (Replaced LSL_Function/negative index/de with LSL_Function/negative index{{#var:lang}})
Jump to navigation Jump to search

Beschreibung

Funktion: string llJsonSetValue( string Json, list Spezifizierer, string Wert );

Bei Erfolg gibt diese Funktion einen neuen JSON - String zurück, bestehend aus Json mit dem neuen Wert (an der Stelle überschrieben) der in Spezifizierer definiert wurde.

Wenn der Funktionsaufruf fehlschlägt (typischerweise weil ein ungültiger Array-Index außerhalb des gültigen Bereichs spezifiziert wurde) wird JSON_INVALID zurückgegeben.

Ein "ungültiger Array-Index außerhalb des gültigen Bereichs" (out of range array index) bedeutet, dass einer der Integer-Spezifizierer größer ist als die Länge des vorhandenen Arrays auf der entsprechenden Ebene mit dem Json Text, oder größer als 0 auf einer Ebene auf der kein Array existiert.

Der spezielle JSON_APPEND - Spezifizierer wird ebenfalls akzeptiert. Er fügt den Wert an das Ende des Arrays in der entsprechenden Ebene (definiert durch Spezifizierer) an. Vorsicht ist geboten, wenn diese Ebene kein Array ist, da der existierende Wert dann überschrieben wird und mit einem Array ersetzt wird, welches dann den Wert an der ersten Index - Stelle (0) enthält.

Im Gegensatz zu Listen und Strings werden negative Indexe in Json-Arrays nicht unterstützt.

Wenn ein existierender "Key" dem Spezifizierer auf der entsprechenden Ebene entspricht, wird dessen Wert mit dem neuen Wert überschrieben (außer wenn Wert der besondere JSON_DELETE Parameter ist).

Existiert an dieser - durch Spezifizierer definierten - Stelle kein Wert, wird ein neues Schlüssel-Wert-Paar innerhalb des Json-Objekts erzeugt.

Um einen existiertenden Wert an der durch Spezifizierer definierten Stelle zu löschen, verwenden Sie als Wert das Schlüsselwort JSON_DELETE. Dieser Vorgang entfernt dabei keine leeren Objekte oder Arrays aus höheren Ebenen.

Wenn Wert gleich JSON_TRUE, JSON_FALSE oder JSON_NULL ist, wird der Wert entsprechend an der durch Spezifizierer definierten Stelle als Text 'true', 'false' oder 'null' innerhalb von Json gesetzt.

• string Json Ausgangsdaten (JSON)
• list Spezifizierer Position an der ein Wert hinzugefügt, geändert oder gelöscht werden soll.
• string Wert Der neue Wert oder JSON_DELETE.

specifiers unterstützt keine negative(n) Indizes.

Spezifikationen

Beispiele

<lsl> string TEST_STRING_JSON;

init() {

   TEST_STRING_JSON = "[9,\"<1,1,1>\",false,{\"A\":8,\"Z\":9}]";

// [9,"<1,1,1>",false,{"A":8,"Z":9}]

   say("Original TEST_STRING_JSON: " + TEST_STRING_JSON);

}

run_json_test(string input) {

   string output;

// changing values within the json string

// change the first value in the array to 10

   output = llJsonSetValue(input, [0], "10");

// [10,"<1,1,1>",false,{"A":8,"Z":9}]

   say("( 1): " + output);

// change the third value in the array to 'true'

   output = llJsonSetValue(input, [2], JSON_TRUE);

// [9,"<1,1,1>",true,{"A":8,"Z":9}]

   say("( 2): " + output);

// change the value of "A" within the Json object to 3

   output = llJsonSetValue(input, [3, "A"], "3");

// [9,"<1,1,1>",false,{"A":3,"Z":9}]

   say("( 3): " + output);

// adding a value or new key-value-pair within the input

// add the value "Hello" to the end of the array // NOTE: One cannot insert, only add to the end

   output = llJsonSetValue(input, [JSON_APPEND], "Hello");

// [9,"<1,1,1>",false,{"A":8,"Z":9},"Hello"]

   say("( 4): " + output);

// add the key-value-pair "B":10 to the object

   output = llJsonSetValue(input, [3, "B"], "10");

// [9,"<1,1,1>",false,{"A":8,"B":10,"Z":9}]

   say("( 5): " + output);


// Things to look out for when modifying Json text // ~!~ Be careful when using this function ~!~

// out of bounds array assignment: // defined as attempting to add a value to a position ... // ...greater than the length of the array (which may be 0) // JSON_APPEND is ALWAYS the preferred way to add to an array

   output = llJsonSetValue(input, [5], "10");

// %EF%B7%90 (URL escaped JSON_INVALID)

   say("( 6): " + llEscapeURL(output));

// BUT, this works, since it is in bounds // (eqivalent to JSON_APPEND in this case)

   output = llJsonSetValue(input, [4], "10");

// [9,"<1,1,1>",false,{"A":8,"Z":9},10]

   say("( 7): " + output);

// careless formation of new arrays // ( the 4 and all subsequent 0's are all in bounds.)

   output = llJsonSetValue(input, [4, 0, 0, 0], "10");

// [9,"<1,1,1>",false,{"A":8,"Z":9},[[[10]]]]

   say("( 8): " + output);

// overwriting an object with an array: // ~!~ mistaken use of JSON_APPEND on an object ~!~

   output = llJsonSetValue(input, [3, JSON_APPEND], "10");

// [9,"<1,1,1>",false,[10]]

   say("( 9): " + output);

// careless formation of new objects // NOTE: "Key" assignemts will NEVER result in a return of JSON_INVALID!

   output = llJsonSetValue(input, [3, "W", "X"], "10");

// [9,"<1,1,1>",false,{"A":8,"W":{"X":10},"Z":9}]

   say("(10): " + output);
   output = llJsonSetValue(input, [3, "W", "X", "Y"], "10");
// [9,"<1,1,1>",false,{"A":8,"W":{"X":{"Y":10

Weiterführende Anmerkungen

Alle Issues

~ Nach JIRA-Issues suchen, die sich hierauf beziehen
   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 llJsonSetValue( string Json, list Spezifizierer, string Wert );
Dieser Artikel war nicht hilfreich für Dich? Vielleicht bringt der zugehörige Artikel im SLinfo Wiki Erleuchtung.

,"Z":9}]

   say("(11): " + output);

// overwriting an array with an object

   output = llJsonSetValue(input, ["X"], "10");

// {"X":10}

   say("(12): " + output);

// special case considerations:


// BUG-3692: (NOTE: Corrected in release 13.09.21.281328!) // a bug where, instead of JSON_INVALID being returned, if the out of // bounds index is at a lower level than the topmost (root) level, a // non-compliant JSON text would be formed

   output = llJsonSetValue(input, [1, 7], "Disappearing Text");

// Note the "empty" second position that resulted in the returned array // [9,,false,{"A":8,"Z":9}] // (But now correctly shows JSON_INVALID)

   say("(13): " + output);

// though there is no way to directly delete a key-value-pair // nor remove a value from an array, // the use of JSON_NULL may prove adequate

   output = llJsonSetValue(input, [3, "A"], JSON_NULL);

// [9,"<1,1,1>",false,{"A":null,"Z":9}]

   say("(14): " + output);

// if a JSON text object has been formed with llList2Json() // that contains one or more duplicated "Keys", (allowable // but NOT recommended!) ANY change // made to that object will correct the condition, // with all but the last such "Key" being removed

   output = llList2Json(JSON_OBJECT, ["A", 1, "A", 2, "A", 3, "B", 4, "B", 4]);

// both Keys "A" and "B" are duplicated // {"A":1,"A":2,"A":3,"B":4,"B":4}

   say("(15): " + output);

// only the last value of the duplications is accessable though

// 3

   say("(16): " + llJsonGetValue(output, ["A"]));

// condition corrected by adding a key-value-pair...

// {"A":3,"B":4,"Z":5}

   say("(17): " + llJsonSetValue(output, ["Z"], "5"));

// ... or by changing a value

// {"A":5,"B":4}

   say("(18): " + llJsonSetValue(output, ["A"], "5"));

}

say(string message) {

   llOwnerSay(message);

// llRegionSayTo(llGetOwner(), PUBLIC_CHANNEL, message); // llWhisper(PUBLIC_CHANNEL, message); }

default {

   on_rez(integer start_param)
   {
       llResetScript();
   }
   state_entry()
   {
       init();
   }
   touch_end(integer num_detected)
   {

// copy 'TEST_STRING_JSON' from the following function call // to the string 'input' in the function declaration // and run a test on 'input' to not (!) modify 'TEST_STRING_JSON' // but its copy instead

       run_json_test(TEST_STRING_JSON);
   }

} </lsl> |caveats= |helpers |also_functions=|-style="vertical-align:top;" | style="color:gray;" |•  | llList2Json | style="color:gray;" | | | |-style="vertical-align:top;" | style="color:gray;" |•  | llJson2List | style="color:gray;" | | | |-style="vertical-align:top;" | style="color:gray;" |•  | llJsonGetValue | style="color:gray;" | | | |-style="vertical-align:top;" | style="color:gray;" |•  | llJsonValueType | style="color:gray;" | | | |also_events |also_tests |also_articles=|-style="vertical-align:top;" | style="color:gray;" |•  | Typecast | style="color:gray;" | | | |notes= |permission |negative_index |sort=JsonSetValue |cat1=List |cat2=String |cat3=Data Conversion |cat4=JSON |history = Veröffentlichungsdatum 20/05/2013 }}