Problem with reading js - Forum

Forum Navigation
You need to log in to create posts and topics.

Problem with reading js

Hi again, I know that is my script is so stupid but how can I resolve the problem?

LoadJSON "https://jaaak.ir/category/jdhhXhZx3G33XWYzj05DN.json" [ns_category_db]
SetVar [ns_category_counter] 0
.=========================================
While 0 <= 2
      SetVar [ns_category[ns_category_counter]_name] [ns_category_db([ns_category_counter]).name]
      SetVar [ns_category[ns_category_counter]_id] [ns_category_db([ns_category_counter]).id]
      SetVar [ns_category[ns_category_counter]_slug] [ns_category_db([ns_category_counter]).slug]
      SetVar [ns_category[ns_category_counter]_statue] [ns_category_db([ns_category_counter]).statue]
      Math "[ns_category_counter]+1" -1 [ns_category_counter]
EndWhile

I want get and read json file like this:

[
  {
    "name": "null",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  },
  {
    "name": "null",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  },
  {
    "name": "null",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  }
]

And then list all "name" into a comobox

Hi @tilesoft,

I don't know what you want to do but surely the condition of the while loop (While 0 <= 2) is wrong. It's an infinite cycle that will never end. Then to loop through a json object use LoopObject (https://webhelp.visualneo.com/Conditional.html) and not the normal while loop.

 

luishp and tilesoft have reacted to this post.
luishptilesoft

Thanks, You are right! @roccocogliano

 

In other words, I want list "name" of this json file to combo box:

[
  {
    "name": "null",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  },
  {
    "name": "null",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  },
  {
    "name": "null",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  }
]

And the number of json items is not clear and any number of items that are available should be listed in the combo box

@tilesoft please check the attached app.

BeginJS
$App.data=[
  {
    "name": "One",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  },
  {
    "name": "Two",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  },
  {
    "name": "Three",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  }
]
EndJS

.Create the array we have assigned to the combobox
CreateEmptyArray [myarray]

.Loop through the JSON data
Loop 0 [data.length-1] [n]
   .Add item to the array
   ArrayAddItem [myarray] [data([n]).name]
EndLoop

Regards.

Uploaded files:
  • You need to login to have access to uploads.
tilesoft has reacted to this post.
tilesoft

Thanks, This worked.

If user selected "name" from item 2 of json, Now how can I read item 2 "id"?

I do this:

ListBoxGetSelectedIndex "Combobox" [selected_index]
SetVar [selected_id] [data([selected_index]).id]

Is it true?

@tilesoft no, it's quite easier. Please check the attached app.
The key point is this:

.Loop through the JSON data
Loop 0 [data.length-1] [n]
   .Add item to the array and the associated value separated by |
   ArrayAddItem [myarray] "[data([n]).name]|[data([n]).id]"
EndLoop

Regards.

 

Uploaded files:
  • You need to login to have access to uploads.
tilesoft has reacted to this post.
tilesoft

Thanks Luis,

And my last question is if user select the name of second item of this json, And we want select all sub-items of user selected such as name, id, slug, statue from json, How can it happen?

.Loop through the JSON data to get all the information from the selected id.
Loop 0 [data.length-1] [n]
   If [data([n]).id] == [selectedId]
      SetVar [name] [data([n]).name]
      SetVar [slug] [data([n]).slug]
      SetVar [statue] [data([n]).statue]
      SetVar [id] [data([n]).id]
      ExitLoop
   EndIf
EndLoop

@tilesoft
Now you have all the information stored in [name], [slug], [statue] and [id]

tilesoft has reacted to this post.
tilesoft

Thanks alot.

I know it's annoying, but please answer this question as well. Now that the user has selected the desired item in the combo box, how can the program delete all information and other items related to that item from the json file?

Now that the user has selected the desired item in the combo box, how can the program delete all information and other items related to that item from the json file?

@tilesoft sorry I don't understand your question

Let me explain another way. Suppose, based on this example,

[
  {
    "name": "null",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  },
  {
    "name": "null",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  },
  {
    "name": "null",
    "id": "null",
    "slug": "null",
    "statue": "Disable"
  }
]

the user chooses the second item. Now, how can the program delete all the elements in the second item such as name, id, slug, statue from the json file?

Actually, my question has two parts.
The first part is, how can the program determine which item the user has chosen?
And the second part is how the program can delete all the elements related to the item selected by the user?

@tilesoft please, note that your sample JSON data has not real data and will not work in a "real" app. The "id" should be "unique" and two diferent items should not share the same id value. That's a key point for working with data and databases. You need a "key" field.
Once you understand this, you can loop through the JSON data and find the record with the "id" you are looking for.
Then you can retrieve any data from that record or modify it.
To better manage data stored in a JSON file you can use neoSQL plugin. It allows to perform any SQL query over JSON data as if it was a real database.

Regards.

 

tilesoft has reacted to this post.
tilesoft