1

I get JSON data in 1C 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.

              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;
JuLe
  • 115
  • 6

2 Answers2

1

Your code should be like this:

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.

enter image description here

enter image description here

Eugene
  • 105
  • 5
0
use curElement.get("Key")

it another type of data

  • Since this is already provided in the accepted answer, I can only assume this was posted as a "thank you" or "confirmation" comment. – Trenton McKinney Jun 28 '22 at 19:45