PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 24, 2022

[FIXED] How to access $value in a JSON?

 July 24, 2022     asp.net-apicontroller, json, reactjs     No comments   

Issue

{
  "$id": "1",
  "listTasks": {
    "$id": "2",
    "$values": [
      {
        "$id": "3",
        "id": 1,
        "name": "Task1G1",
        "priorityID": 1,
        "dueDate": "2022-07-14T04:12:14.114",
        "createdAt": "2022-07-14T11:13:06.1808811",
        "enumerable": {
          "$id": "4",
          "$values": [
            2
          ]
        }
      },
      {
        "$id": "5",
        "id": 2,
        "name": "string",
        "priorityID": 1,
        "dueDate": "2022-07-29T08:55:06.156",
        "createdAt": "2022-07-14T15:55:57.0330615",
        "enumerable": {
          "$id": "6",
          "$values": [
            3
          ]
        }
      }
    ]
  }
}

I want to access the secondary $value in this json (enumerable object), I use this code below:

useEffect(() => {
      axios
         .get(API_URL + Task/GetTaskInGroup?GroupID=${id}, {})
         .then((response) => {
            **setTaskList(response.data.listTasks.$values.enumerable.$value);**
         })
         .catch((error) => {
            console.log(error.response.data);
         });
   }, [id]);

But it didn't work, I tried setTaskList(response.data.listTasks.$values) then it work (I can get all data but I can't access to enumerable object)


Solution

Try with computed property names (myObject[..]) like below:

response.data.listTasks["$values"][0].enumerable["$values"]

const data = { "$id": "1", "listTasks": { "$id": "2", "$values": [ { "$id": "3", "id": 1, "name": "Task1G1", "priorityID": 1, "dueDate": "2022-07-14T04:12:14.114", "createdAt": "2022-07-14T11:13:06.1808811", "enumerable": { "$id": "4", "$values": [ 2 ] } }, { "$id": "5", "id": 2, "name": "string", "priorityID": 1, "dueDate": "2022-07-29T08:55:06.156", "createdAt": "2022-07-14T15:55:57.0330615", "enumerable": { "$id": "6", "$values": [ 3 ] } } ] } }

console.log(data.listTasks["$values"][0].enumerable["$values"]);



Answered By - Amila Senadheera
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing