How to parse json formatted string?

The 1C:Enterprise developers forum

#1
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 6, 2018
Company: Bodega LLC

Hello!

I need to write "json formatted string data" in a value table. Could you help me plz..

 
#2
People who like this:0Yes/0No
Interested
Rating: 16
Joined: Dec 4, 2017
Company:

Dear eyup yilmaz,

For instance, you have a JSON structure like this:

Code
[
   {
      "name": "John",
      "surname": "Stuart",
      "age": 40
   },
   {
      "name": "Katty",
      "surname": "Black",
      "age": 25
   }
]


The code you can use to parse this simple structure is as follows:

Code
JSONReader = New JSONReader();
JSONReader.OpenFile("FileName");
Employees = ReadJSON(JSONReader);
JSONReader.Close();

For Each Employee In Employees Do
// Process employee data
EmployeeName = Employee.name;
EmployeeSurname = Employee.surname;
EmployeeAge = Employee.age;

EndDo;



Also, pay attention to the description of the ReadJSON() global context method, which is available in Syntax Assistant.

Best regards,
Vladimir Gurov

1C Company support team
 
#3
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 6, 2018
Company: Bodega LLC

Actually i have string variable not file;

{"ProductCode2":{"Kg":"5","Name":"something1"},"ProductCode2":{"Kg":"2","Name":"something2"}}

I could not use JSONReader()...

 
#4
People who like this:0Yes/0No
Interested
Rating: 16
Joined: Dec 4, 2017
Company:

Dear eyup yilmaz,

You can take a look at Syntax Assistant.

For instance:

Quote

JSONReader
SetString
Syntax:

SetString(<JSONString>)
Parameters:

<JSONString> (required)

Type: String.
A string, containing text in JSON format.
Description:

Defines the row containing JSON text to be read by the given object. If JSON has already been read from another file or rows before this method is called up, the reading will stop and the object initializes for reading from the row shown.

Availability:

Thin client, Mobile client, server, thick client, external connection, Mobile application (client), Mobile application (server).
Example:

JSONReader = New JSONReader;
JSONReader.SetString("{}");



Best regards,
Vladimir Gurov

1C Company support team
 
#5
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 6, 2018
Company: Bodega LLC

Actually i tried "SetString", but it did not work... I solved by writing an algorithm...

Code
a = StrSplit(res,"""");
   y = 0;
   z = 0;
   array = new array;
   For Each x in a Do
      if Not x = "," then
         if x = "{" then
            y = 1;   
         elsif x = ":{" then
            y = 2;
         elsif x = "}," then
            if z = 0 then
               for i = 0 to array.Count()-1 do
                  b = "a" + String(i);
                  VT.Columns.Add(b);
               EndDo;
               z = 1;
            endif;
            Row = VT.Add();
            for i = 0 to (array.Count()-1) do
               Row.Set(i,array[i]);
            enddo;
            array.Clear();
            y = 3;
         elsif x = "}}" then
            Row = VT.Add();
            for i = 0 to (array.Count()-1) do
               Row.Set(i,array[i]);
            enddo;            
            array.Clear();
            y = 4;
         endif;
         if y = 1 AND Not x = "{"  AND Not x = ":" then
            array.Add(x); 
         elsif y = 2 AND Not x = ":{" AND Not x = ":" then
            array.Add(x);
         elsif y = 3 AND Not x = "}," then
            array.Add(x);
         elsif y = 4 AND Not x = "}}" then
            
         endif;
      endif;

 
#6
People who like this:0Yes/0No
Just came
Rating: 1
Joined: Sep 17, 2018
Company: Adinsoft

eyup yilmaz,

Just put that json string in to temp json file and read with JSONReader. After that you can easily use for each loop like Vladimirs example.

Code

MyJsonString = "{"ProductCode2":{"Kg":"5","Name":"something1"},"ProductCode2":{"Kg":"2","Name":"something2"}} ";
JsonFile= GetTempFileName("json");      
Json2Text= New TextDocument();
Json2Text.AddLine(MyJsonString);
Json2Text.Write(JsonFile);
JSONReader = New JSONReader();
JSONReader.OpenFile(JsonFile);
Employees = ReadJSON(JSONReader);
JSONReader.Close();
DeleteFiles(JsonFile,);

For Each Employee In Employees Do

// Process employee data

EmployeeName = Employee.name;
EmployeeSurname = Employee.surname;
EmployeeAge = Employee.age;
EndDo;

Edited: ugur emir - Nov 27, 2018 06:15 AM
 
#7
People who like this:0Yes/0No
Active user
Rating: 2
Joined: Aug 28, 2015
Company:

Hello.
You don't need to write your json string to a file and then read the file.
Actually,there is more convenient way to parse it:

YourJSONString = "{"ProductCode2":{"Kg":"5","Name":"something1"},"ProductCode2":{"Kg":"2","Name":"something2"}}";

JSONReader = New JSONReader();
JSONReader.SetString(YourJSONString );
JsonStructure = ReadJSON(JSONReader );
JSONReader.Close();

 
#8
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 6, 2018
Company: Bodega LLC

I had an error;

Code
YourJSONString = "{"ProductCode2":{"Kg":"5","Name":"something1"},"ProductCode2":{"Kg":"2","Name":"something2"}}";

JSONReader = New JSONReader();
JSONReader.SetString(YourJSONString );
JsonStructure = ReadJSON(JSONReader );
JSONReader.Close();



{CommonModule.Firebase.Module(134)}: Error calling context method (ReadJSON)
JsonStructure = ReadJSON(JSONReader );
, reason:
Invalid property name "7 DNEI" is encountered when reading JSON data to a Structure object

, reason:
Invalid property name "7 DNEI" is encountered when reading JSON data to a Structure object

 
#9
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 6, 2018
Company: Bodega LLC

This is my json string...

 
#10
People who like this:0Yes/0No
Just came
Rating: 1
Joined: Sep 17, 2018
Company: Adinsoft

I had same issue when i use SetString and finally found solution to write temp file and read from file.

 
#11
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 6, 2018
Company: Bodega LLC

Again i get same error. What do you think my json format maybe wrong? I am getting it from firebase database wtih this code;

Code
WinHttp = New COMObject ("WinHttp.WinHttpRequest.5.1"); 
   WinHttp.Option (2, "utf-8"); 
   WinHttp.Open (Method, Request, 0); 
   WinHttp.SetRequestHeader ("Accept-Language", "en"); 
   WinHttp.SetRequestHeader ("Accept-Charset", "utf-8"); 
   WinHttp.setRequestHeader ("Content-Language", "en"); 
   WinHttp.setRequestHeader ("Content-Charset", "utf-8"); 
   WinHttp.setRequestHeader ("Content-Type", "application / json; charset = utf-8"); 
   WinHttp.Send (); 
   res = WinHttp.ResponseText();
   //JSONReader = New JSONReader();
   //JSONReader.SetString(res);
   //JsonStructure = ReadJSON(JSONReader );
   //JSONReader.Close();

   JsonFile= GetTempFileName("json");      

   Json2Text= New TextDocument();

   Json2Text.AddLine(res);

   Json2Text.Write(JsonFile);

   JSONReader = New JSONReader();

   JSONReader.OpenFile(JsonFile);

   Employees = ReadJSON(JSONReader);

   JSONReader.Close();

   DeleteFiles(JsonFile,);



res is my json string...

 
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.