godot c# using Array , Dictionary

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By karismaup

It appears there is something wrong in converting the dictionary to a JSON string, as the project crashes when trying to convert dictionaries. Converting a string to JSON works fine, as does storing a string to a file.

I get the following error: drivers/unix/net_socket_posix.cpp:190 - Socket error: 10054

  • Server disconnected! when converting a Dictionary into a JSON string. The user on the forums gets the following error: drivers/windows/stream_peer_tcp_winstock.cpp:203 - Server disconnected!, which seems like the same issue, just for different operating systems
 if( fileChecker( _jsonPath ) )
  {
            File jsonfile = new File();
            jsonfile.Open( _jsonPath , (int)File.ModeFlags.Read );
            string jsonString = jsonfile.GetAsText();
            JSONParseResult dict = JSON.Parse(jsonString);
            jsonfile.Close();
    
            if( dict.Error != 0 )
            {
                GD.Print( "Error:" , dict.Error );
            }else{
                Array parsed = dict.Result as Array;
                var nodeScene = parsed.GetValue(0) as Dictionary<object , object>;
                //Error
            }
            
 };
:bust_in_silhouette: Reply From: karismaup

==================I solved the problem==================

==================JSON=====================
[
{
“scene0” : “aaaaaaaaaaa0”
},
{
“scene1” : “aaaaaaaaaaa1”
},
{
“scene2” : “aaaaaaaaaaa2”
},
{
“scene3” : “aaaaaaaaaaa3”
},
{
“scene4” : “aaaaaaaaaaa4”
}
]

=============================================

using Godot;
using System;
using SC = System.Collections.Generic;
using GC = Godot.Collections;

struct JsonData
{
    public String scene;
}

public class Main : Node2D
{
    JsonData jsonData;

    public void Jsonloader( String _jsonPath )
    {
            var parseResult = GetJSONParseResult( _jsonPath );
            var sceneData = parseResult.Result as GC.Array;
            for( int i = 0; i < sceneData.Count; i++ )
            {
                var Data = sceneData[i] as GC.Dictionary;
                jsonData.scene = Data["scene" + i ] as String;
                GD.Print( jsonData.scene );
            }
    }

    private JSONParseResult GetJSONParseResult(string localFileName)
    {
        var file = new File();
        file.Open(localFileName, 1);  // 1 is read only
        var text = file.GetAsText();
        file.Close();

        var result = JSON.Parse(text);

        if(result.Error != 0)
        {
            GD.Print($"There was an error: {result.Error}");
            return null;
        }
        else
        {
            GD.Print("JSON file read ok.");
            return result;
        }
    }
}