0 votes

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/netsocketposix.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/streampeertcp_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
            }

 };
asked Jun 19 in Projects by karismaup (22 points)

1 Answer

0 votes

==================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;
        }
    }
}
answered Jun 24 by karismaup (22 points)
edited Jun 24 by karismaup
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.