Jump to content

Another json question


Innula Zenovka
 Share

You are about to reply to a thread that has been inactive for 470 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

30 minutes ago, Innula Zenovka said:

I think it ought to be llJsonGetValue("test",["id",0]); but that doesn't work, and neither does anything else I try.

"test" is a string which contains no JSON data. instead try

llJsonGetValue(test,["playlists",0,"id"]);

Maybe try looking at it one level at a time (the indentation on all these examples is horrible)

test is a JSON object containing one key value pair

Quote
{   "playlists": ...
}

 

to fetch the "..." you descend one level and ask for the "playlists", the value of which is

Quote

[ ... ,  ... ]

a JSON array, to descend one more level, you ask for an index. at index 0, the ... is

Quote
{   "id": ...
    "title": ...
    "description": ...
    "trackCount": ...
}

a JSON object, and to get one of the ...'s you ask for the key, for example "id".

Edited by Quistess Alpha
  • Thanks 1
Link to comment
Share on other sites

Just as an example, you can also use LSL to construct JSON strings:

        string JSON;
        JSON = llJsonSetValue(JSON,["playlists",0,"id"],"ID_1");
        JSON = llJsonSetValue(JSON,["playlists",0,"title"],"TITLE_1");
        JSON = llJsonSetValue(JSON,["playlists",0,"description"],"DESC_1");
        JSON = llJsonSetValue(JSON,["playlists",0,"playcount"],"6");
        JSON = llJsonSetValue(JSON,["playlists",1,"id"],"ID_2");
        JSON = llJsonSetValue(JSON,["playlists",1,"title"],"TITLE_2");
        JSON = llJsonSetValue(JSON,["playlists",1,"description"],"DESC_2");
        JSON = llJsonSetValue(JSON,["playlists",1,"playcount"],"8");
        llOwnerSay(JSON);

 

Link to comment
Share on other sites

58 minutes ago, Love Zhaoying said:

"test" is the name of your variable.

Try:  llJsonGetValue(test, [..what to search for..]);

"id" is not an array, so "0" won't find anything.

What do you want to find?  

"Playlists" is an array, so llJsonGetValue(test, [playlists, 0, id]) would get the first playlist, and the value of "id".

No matter what you are looking for, it appears "playlists" is your only top-level entry. So, no matter what you are looking for, it would have to be ["playlists",..].

Since you only have 2 "playlists" array entries, your choices are ["playlists",0..] and ["playlists",1..].

First level: "playlists": Key for KVP, of which the "value" is JSON_ARRAY.

Next level: JSON_ARRAY, so you must provide an "index" to select an entry in the JSON_ARRAY

Next level: JSON_OBJECT. so provide a "key"..

 

 

 

Sorry, but I'm still not getting it.   I think you're telling me to use

string test = "{
    \"playlists\": [{
        \"id\": \"some key\",
        \"title\": \"Playlist 1\",
        \"description\": null,
        \"trackCount\": 20
    }, {
        \"id\": \"another key\",
        \"title\": \"Playlist 2\",
        \"description\": null,
        \"trackCount\": 30
    }
    ]
}";

default
{
    state_entry()
    {
        llOwnerSay(llJsonGetValue("test",["playlists",0,"id"]));
    }
   
}

but that can't be it, because it returns "[]".   Clearly I'm misunderstanding something somewhere.

  • Like 1
Link to comment
Share on other sites

4 minutes ago, Innula Zenovka said:

Clearly I'm misunderstanding something somewhere.

you're not putting the variable into the function, you're putting in the string "test" which is not valid JSON.

llOwnerSay(llJsonGetValue(test,["playlists",0,"id"])); // no quotes.

Edited by Quistess Alpha
  • Thanks 2
Link to comment
Share on other sites

7 minutes ago, Quistess Alpha said:

you're not putting the variable into the function, you're putting in the string "test" which is not valid JSON.

llOwnerSay(llJsonGetValue(test,["playlists",0,"id"])); // no quotes.

^^ yes, what I said in my first answer earlier! 🙂 

@Innula ZenovkaYou can pass "any string" to llJsonGetValue(), and it won't give you a run-time error. So, perhaps you "meant" to pass the variable test but are passing "test" instead?

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 470 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...