フィルターのクリア

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

4 ビュー (過去 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 Center および File ExchangeRead Data from Channel についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by