The 1C:Enterprise developers forum

#1
People who like this: 0 Yes / 0 No
Active user
Rating: 3
Joined: Apr 10, 2020
Company: HerSüreçYazılım

Hello
You get json data from the address written below. There is no problem when there is only one registration. But I cannot list many records. It gives an error "Object field not found (Key)". What do I have to do to list the records? Help please.

Code;
              Host     = "jsonplaceholder.typicode.com/";
          HTTPRequest = New HTTPRequest;
              // HTTPRequest.ResourceAddress = "todos/1";
               HTTPRequest.ResourceAddress = "photos/" ;// ThisObject.Attribute2;
               HTTPConnection         = New HTTPConnection(host,,,,,10,New OpenSSLSecureConnection);
               HTTPAnswer               = HTTPConnection.Get(HTTPRequest);
               stringAnswer= HTTPAnswer.GetBodyAsString();
               JSONReader = New JSONReader;
               JSONReader.SetString(stringAnswer);
               JsonResult = ReadJson(JSONReader,True);      

                For each strResult in JsonResult Do
                    If (strResult.Key = "url") Then
                        Message(strResult.Value);                                                  
                     EndIf;
               EndDo;

 
#2
People who like this: 0 Yes / 0 No
Administrator
Rating: 29
Joined: Oct 3, 2019
Company:

Hello Ertuğrul,

most likely the problem is that there are other fields in the structure you received, not just "Key".

To avoid the error, before executing the

Code
If (strResult.Key ="url") Then


code, you must check for the existence of such a field. For example, you can do it like this:

Code
For each strResult in JsonResult Do
   If strResult.Property("Key") Then 
      If (strResult.Key = "url") Then
         Message(strResult.Value);                                                  
      EndIf;
   EndIf;
EndDo;

 
#3
People who like this: 0 Yes / 0 No
Active user
Rating: 3
Joined: Apr 10, 2020
Company: HerSüreçYazılım

Hello,

This gave the error.
"Object method not found (Property)"

 
#4
People who like this: 0 Yes / 0 No
Administrator
Rating: 29
Joined: Oct 3, 2019
Company:

ok, then mail me please your source code and test account for "jsonplaceholder.typicode.com".

I'll deal with it

Aleksandr

 
#5
People who like this: 0 Yes / 0 No
Administrator
Rating: 29
Joined: Oct 3, 2019
Company:

My mail: abir@1c.com

 
#6
People who like this: 0 Yes / 0 No
Administrator
Rating: 29
Joined: Oct 3, 2019
Company:

Ertuğrul,

I understood where the bug is.

So your code should be like this:

Code
   Host     = "jsonplaceholder.typicode.com/";
   HTTPRequest = New HTTPRequest;
   HTTPRequest.ResourceAddress = "photos/" ;
   HTTPConnection   = New HTTPConnection(host,,,,,10,New OpenSSLSecureConnection);
   HTTPAnswer      = HTTPConnection.Get(HTTPRequest);
   stringAnswer   = HTTPAnswer.GetBodyAsString();
   JSONReader       = New JSONReader;
   JSONReader.SetString(stringAnswer);
   JsonResult = ReadJson(JSONReader,True);      
   
   For each strResult in JsonResult Do
      For Each curElement In strResult Do 
         If (curElement.Key = "url") Then
            Message(curElement.Value);
            Break; // since the value curElement.Key = "url" can be only once, we can exit the loop
         EndIf;
      EndDo;
   EndDo;   




JsonResult is an array of values (see scr.1). Each element of the array is a map strResult (scr.2). First, in a loop, we iterate over all the elements of the array, and in a nested loop, we iterate over the matching fields.

Download 1.png (34.89 KB)
Download 2.png (30.78 KB)
 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)
Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.

/* Улучшаем читаемость списков в документации */ .workarea ul, .content ul { margin: 15px 0 20px 25px !important; padding: 0 !important; list-style-type: disc !important; } .workarea li, .content li { margin-bottom: 8px !important; padding-left: 5px !important; line-height: 1.6 !important; } /* Ультимативный фикс для всех уровней заголовков в контенте */ .workarea h1, .workarea h2, .workarea h3, .workarea h4, .workarea h5, .workarea h6, .content h1, .content h2, .content h3, .content h4, .content h5, .content h6, div[id*="content"] h5, div[class*="content"] h5 { display: block !important; font-weight: 700 !important; color: #005aa0 !important; /* Наш фирменный синий */ text-transform: none !important; margin-top: 30px !important; margin-bottom: 15px !important; line-height: 1.2 !important; font-family: Arial, sans-serif !important; } /* Принудительные размеры в пикселях (чтобы не зависеть от % или em) */ .workarea h1, .content h1 { font-size: 28px !important; } .workarea h2, .content h2 { font-size: 24px !important; } .workarea h3, .content h3 { font-size: 21px !important; } .workarea h4, .content h4 { font-size: 19px !important; } .workarea h5, .content h5 { font-size: 18px !important; } /* Для 5 решеток ##### */ .workarea h6, .content h6 { font-size: 16px !important; } /* Фикс, если Битрикс добавил лишние отступы перед списками после заголовка */ .workarea h5 + ul, .content h5 + ul { margin-top: 5px !important; }