JSON problem

The 1C:Enterprise developers forum

#1
People who like this:0Yes/0No
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:0Yes/0No
Administrator
Rating: 23
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:0Yes/0No
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:0Yes/0No
Administrator
Rating: 23
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:0Yes/0No
Administrator
Rating: 23
Joined: Oct 3, 2019
Company:

My mail: abir@1c.com

 
#6
People who like this:0Yes/0No
Administrator
Rating: 23
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.