Desktop version

Main > Forum > 1C:Enterprise Platform > 1C:Enterprise – Business applications platform > JSON problem

Forum

Search UsersRules
JSON problem
#1
Active user
Points:: 0
Joined:: Apr 10, 2020

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;

Profile
#2
Administrator
Points:: 0
Joined:: Oct 3, 2019

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;

Profile
#3
Active user
Points:: 0
Joined:: Apr 10, 2020

Hello,

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

Profile
#4
Administrator
Points:: 0
Joined:: Oct 3, 2019

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

I'll deal with it

Aleksandr

Profile
#5
Administrator
Points:: 0
Joined:: Oct 3, 2019

My mail: abir@1c.com

Profile
#6
Administrator
Points:: 0
Joined:: Oct 3, 2019

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.

Profile
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)



© 1C LLC. All rights reserved
1C Company respects the privacy of our customers and visitors
to our Web-site.