more G-Labs products

Author Topic: Help Parse Web JSON data  (Read 10542 times)

January 07, 2015, 10:48:50 AM
Reply #30

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31
Hello!

I'm still blocked, cannot understand very well how it works.
As seen, I can get vaules with the previous code I've implemented. Using program and virtual modules I was able to create them and collect informations. Googling on this forum as well as on the sourceforge forum I've found how to query the modules:

http://<MY_IP>/api/HomeAutomation.HomeGenie/Config/Modules.List/

This produces a long list. Let me take this block:

Code: [Select]
{
   "Name": "xefil - XefilHomeArduino",
   "Description": "",
   "DeviceType": "Program",
   "Domain": "HomeAutomation.HomeGenie.Automation",
   "Address": "1007",
   "Properties": [

(..snip...)

       {
           "Name": "xefil.XefilHomeArduino.LeafNodeId01.Relay01",
           "Description": "",
           "Value": "0",
           "UpdateTime": "2015-01-07 09:37:09Z",
           "ValueIncrement": "0",
           "LastValue": "0",
           "LastUpdateTime": "2015-01-07 09:35:12Z"
       },

(..snip...)

       {
           "Name": "xefil.XefilHomeArduino.LeafNodeId01.DHT11_H",
           "Description": "",
           "Value": "36",
           "UpdateTime": "2015-01-07 09:37:09Z",
           "ValueIncrement": "0",
           "LastValue": "36",
           "LastUpdateTime": "2015-01-07 09:35:12Z"
       },
       {
           "Name": "xefil.XefilHomeArduino.LeafNodeId01.DHT11_T",
           "Description": "",
           "Value": "25",
           "UpdateTime": "2015-01-07 09:37:09Z",
           "ValueIncrement": "0",
           "LastValue": "25",
           "LastUpdateTime": "2015-01-07 09:35:12Z"
       },

(..snip...)


To start understanding, how could I create a generic switch that simply write to the console the 'xefil.XefilHomeArduino.LeafNodeId01.DHT11_H' if the switch is ON and the 'xefil.XefilHomeArduino.LeafNodeId01.DHT11_T' if the switch is OFF?

I know, this doesn't make much sense, but it's only a start point to inderstand how to query already collected values. I'll use them later to query the relay status and do something with that later.


Where should I get the values? From here?

Code: [Select]
   "Name": "xefil - XefilHomeArduino",
   "Description": "",
   "DeviceType": "Program",
   "Domain": "HomeAutomation.HomeGenie.Automation",
   "Address": "1007"

Or from here?

Code: [Select]
   "Name": "Corridoio DHT11",
   "Description": "",
   "DeviceType": "Sensor",
   "Domain": "HomeAutomation.XefilHomeArduino",
   "Address": "1"

Also is better to get vaules from the program or from the sensor Widget? Or it's the same?

Thanks again, really!


Simon

August 03, 2015, 06:29:16 PM
Reply #31

djatie

  • **
  • Information
  • Jr. Member
  • Posts: 34
Waiting for answer too. I need the value to to somthing with my automation. i hop somebody can answer. :)

August 08, 2015, 11:26:36 AM
Reply #32

djatie

  • **
  • Information
  • Jr. Member
  • Posts: 34

August 08, 2015, 02:37:56 PM
Reply #33

nolio

  • *****
  • Information
  • Global Moderator
  • Posts: 544
Hi,
Did you take a look here ?
http://www.homegenie.it/docs/api/overview.html

I remember last time i found the correct API, i used the "record macro" in action menu. In my case : /api/HomeAutomation.HomeGenie.Automation/90/Control.On/1393535869092.

Control.On/139353586909 isn't very easy to find ... ;)

About "HomeAutomation.XefilHomeArduino" versus "HomeAutomation.HomeGenie.Automation", i don't know but perhaps you can try or use the more easy ...

Not sure to help ...
Bye

August 09, 2015, 01:48:56 AM
Reply #34

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hey nolio,

you don't need that 139353586909 number at all. Those numbers in the end HG client API calls are just put to ensure that the web browser is not caching the request. It's old stuff anyway. Use the standard API syntax as explained here http://www.homegenie.it/docs/api/ .



g.

August 10, 2015, 06:26:24 PM
Reply #35

djatie

  • **
  • Information
  • Jr. Member
  • Posts: 34
this my gpio case control to call from web api

                                  case "Control.Value":
                                 
                                  //var iplocation = Net.WebService("169.254.122.100:8080/api/HomeAutomation.HomeGenie/Config/Modules.Get/RaspberryPi.Gpio/GPIO23/").GetData();
                                 
                                 
                                    WebClient n = new WebClient();
                                    var json = n.DownloadString("http://169.254.122.100:8080/api/HomeAutomation.HomeGenie/Config/Modules.Get/RaspberryPi.Gpio/GPIO23");
                                    string valueOriginal = Convert.ToString(json);
                                 
                                    JObject rss = JObject.Parse(json);

                           //string rssDomain = (string)rss["Domain"];   //working
                                    string itemProperties = (string)rss["Properties"]["Name"][11]; //not working
                                    //JArray status = (JArray)rss["Name"]["Properties"][11]["Name"]; //not working
                                     //string result1 = string.Join("", status); //not working
                                     //IList<string> categoriesText = status.Select(c => (string)c).ToList(); //not working
                                                             
                                  res = itemProperties;
                                  break;
                                 
                                } 
                              //return "{ 'AResponseValue' : 'OK' }";   

i call it use web api
http://169.254.122.100:8080/api/RaspberryPi.Gpio/GPIO23/Control.Value
its  show like this

Accessed JArray values with invalid key value: "Name". Array position index expected.   at Newtonsoft.Json.Linq.JArray.get_Item (System.Object key) [0x00000] in <filename unknown>:0
  at HomeGenie.Automation.Scripting.ScriptingInstance+<RunCode>c__AnonStorey0.<>m__2 (System.Object args) [0x00000] in <filename unknown>:0

can help to get data on Properties? for the example "update time:" on "Homegenie scheduled control On", thx for help  :)

{
   "Name": "",
   "Description": "",
   "DeviceType": "Switch",
   "Domain": "RaspberryPi.Gpio",
   "Address": "GPIO23",
   "Properties": [
       {
           "Name": "HomeGenie.ScheduleControl",
           "Description": "",
           "Value": "On",
           "UpdateTime": "2015-08-09 19:53:50Z"
       },

August 10, 2015, 09:39:22 PM
Reply #36

nolio

  • *****
  • Information
  • Global Moderator
  • Posts: 544
you don't need that 139353586909 number at all. Those numbers in the end HG client API calls are just put to ensure that the web browser is not caching the request. It's old stuff anyway. Use the standard API syntax as explained here http://www.homegenie.it/docs/api/.

Ok i will try without and change my severals call to this API :).
Thanks.

December 13, 2016, 04:13:19 PM
Reply #37

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Hi,

I'd like to get the value out of this json response

json({
   "version": 16,
   "type": "get_state_sensor",
   "sensor": {
      "number": 2,
      "name": "Temp",
      "type": "temperature",
      "value": 21.5,
      "state": [],
      "unit": "°C",
      "utime": 1481641629,
      "date": {
         "weekday": "tu",
         "day": 13,
         "month": 12,
         "year": 2016
      },
      "time": {
         "hour": 16,
         "min": 7,
         "sec": 9
      }
   }
})

I tried with

var SensorData = Net.WebService(webserviceurl).GetData();
string Temperature = int.Parse(SensorData.sensor.value.ToString());

Which is unfortunate not working.

Could the json(....) be the problem? I have no chance to get rid of it.

Anybody any idea?

Many thanks
Cheers
Jens



January 28, 2017, 08:48:06 PM
Reply #38

Bounz

  • ***
  • Information
  • Full Member
  • Posts: 94
Hi, Jens.

Yes, the json(....) is the problem, as it's not a valid JSON object. You need that your response contains only inner part, {...}.

January 31, 2017, 01:17:24 PM
Reply #39

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Hi Bounz,

this is what I assumed, unfortunate there's no way in getting rid of this, as the firmware is not accessible in any way.

Thanks
Jens

January 31, 2017, 02:21:50 PM
Reply #40

raptorjr

  • ***
  • Information
  • Full Member
  • Posts: 78
You don't have access to any string manipulation functions where you receive this JSON? Shouldn't be that hard to remove everything before, and including, the first ( and the the same with the last ) if you have that.
Don't know anything about JSON, but looking at your example it doesn't look like ( and ) is part of a JSON reply. So there won't be a case where searcing for these and remove everything before and after would a problem in the future.

But I'm just throwing out ideas =)