How to read a JSON file?
sovekira
Posts: 6
Hello,
I would like to read a Json file in Daz script,but here's some problem!!
when I tap the oData in the script, I got the right value -0.1102951 in the first print and 0 in the second print.
var oData = { "0Posx" : -0.1102951, "0Posy" : 107.0425, "0Posz" : -12.28755, "0Rotx" : 0.02013095, "0Roty" : -0.0003168428, "0Rotz" : -0.007650683, "0Sclx" : 0, "0Scly" : 0, "0Sclz" : 0} var oSettings = new DzSettings();print( oSettings.getFloatValue( "0Posx" ))if( oSettings.fromString( JSON.stringify( oData ) ) ){ print( oSettings.getFloatValue( "0Posx" ) ); }}
but when I tried to read the oData from json file, use getFloatValue, I got 0 in the first print whatever the true value is and nothing returned in the second print.
var file = new DzFile("C:/Users/momo/Desktop/pose.json");var oData = file.read()var oSettings = new DzSettings();print( oSettings.getFloatValue( "0Posx" ))if( oSettings.fromString( JSON.stringify( oData ) ) ){ print( oSettings.getFloatValue( "0Posx" ) ); }}
I just want to read the value from a json file, please help!!
THANK YOU for replies!!
json
json
pose.json
33K
Comments
Redacted
so, Is is right if I simply want to read a file? I got nothing in print1 and error carse in print2 " Unable to parse JSON string"...
You haven't opened the file for reading, which may not be helping
The forum is having issues displaying code blocks right now, but if you click on "Quote", you will be able to see this properly formatted.
Thanks for your replies!
I can read json now but still have another question...
I want to use the script to get the value by the key, like the example here,DzSettings [Documentation Center] (daz3d.com)
but just got "0" in print( oSettings.getFloatValue( "0Posx" ) );
Not sure why you're still trying to use DzSettings, it has a specific purpose unrelated to what you've stated you are trying to do.
I'm new to Daz script and use DzSettings because can't find other samples which function same like getFloatValue("0Posx")
I tried the script you post here but got the error message Error reading JSON file 'C:/Users/momo/Desktop/pose.json': Unable to parse JSON.
Try using a known good file, such as a .duf preset, for testing - once that is working and you know your code is formally correct you can go on to hand-rolled (or custom saved) JSON files.