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

조회 수: 2 (최근 30일)
Gcobani Mkontwana
Gcobani Mkontwana 2019년 11월 6일
답변: Yousef 2023년 9월 4일
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 커뮤니티

카테고리

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