How to parse json formatted string?


If you wish to leave comments or ask questions, please log on or sign up.

The 1C:Enterprise developers forum

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

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
Administrator
Rating: 18
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:

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
Administrator
Rating: 18
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:

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: 0
Joined: Sep 17, 2018
Company:

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
Just came
Rating: 1
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:

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:

This is my json string...

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

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:

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