How to subscribe to a channel using Jquery to read data in your channel

2 次查看(过去 30 天)
Hi Team
I am trying to read API key to my channel, using jquery to get JSON response to my channel. Here is my jquery logic, i am unable to get a response to my channel i have created and i want to read one field from my channel (field8) only.
<script
src="https://code.jquery.com/jquery-3.3.1.js"integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="crossorigin="anonymous"></script>
<script type="text/javascript">
function loadData(){
var id;
//get the data from thingspeak.
$.getJSON('https://api.thingspeak.com/channels/899906/fields/8.json?api_key=F35GLOFJ8L99D0OM&results=2',function(data) {
id = data.field8;
if(id){
displayData(id);
}
});
}
</script>

回答(1 个)

Yousef
Yousef 2023-9-4
Here is how you can read a specific field from a JSON response in MATLAB using the JSONlab toolbox:
  1. Install JSONlab toolbox if you don't already have it:
pkg install -forge jsonlab
  1. Make a GET request to your API endpoint to get the JSON response:
url = 'https://api.thingspeak.com/channels/YOUR_CHANNEL_ID/feeds.json?api_key=YOUR_API_KEY';
json_text = webread(url);
  1. Parse the JSON text into a MATLAB struct:
json = jsondecode(json_text);
  1. Access the specific field you want from the struct (field8 in this example):
field8_data = json.feeds(1).field8;
The field8_data variable will now contain the data for field8 from the first entry in the feeds array.
You can loop through all entries to access field8 for each:
for i = 1:length(json.feeds)
field8(i) = json.feeds(i).field8;
end

社区

更多回答在  ThingSpeak Community

类别

Help CenterFile Exchange 中查找有关 Read Data from Channel 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by