Desktop version

Main > Forum > 1C:Enterprise Platform > 1C:Enterprise – Business applications platform > How to parse json formatted string?

Forum

Search UsersRules
How to parse json formatted string?
#1
Active user
Points:: 0
Joined:: Mar 6, 2018

Hello!

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

Profile
#2
Interested
Points:: 0
Joined:: Dec 4, 2017

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

Profile
#3
Active user
Points:: 0
Joined:: Mar 6, 2018

Actually i have string variable not file;

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

I could not use JSONReader()...

Profile
#4
Interested
Points:: 0
Joined:: Dec 4, 2017

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

Profile
#5
Active user
Points:: 0
Joined:: Mar 6, 2018

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;

Profile
#6
Just came
Points:: 0
Joined:: Sep 17, 2018

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;

Profile
#7
Active user
Points:: 0
Joined:: Aug 28, 2015

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();

Profile
#8
Active user
Points:: 0
Joined:: Mar 6, 2018

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

Profile
#9
Active user
Points:: 0
Joined:: Mar 6, 2018

This is my json string...

Profile
#10
Just came
Points:: 0
Joined:: Sep 17, 2018

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

Profile
#11
Active user
Points:: 0
Joined:: Mar 6, 2018

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...

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.