Raised This Month: $51 Target: $400
 12% 

JSON decoding


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ludak
Member
Join Date: Oct 2014
Old 06-16-2015 , 07:17   JSON decoding
Reply With Quote #1

Hi everyone, i have tried this include: https://forums.alliedmods.net/showthread.php?t=189772
But Decoding is not working.
Friend told me that i can decode JSON with strtok function
He send me this:
Code:
static szLeft[ 64 ], szRight[ 64 ];
trim( szInput );
replace_all( szInput, charsmax( szInput ), "^"", "" );
replace_all( szInput, charsmax( szInput ), ",", "" );
strtok( szInput, szLeft, charsmax( szLeft ), szRight, charsmax( szRight ), ':' );
How i can use this code?
And format of JSON text that i want to decode is:
Code:
{
     "Something": 1,
     "Something": "abc",
     "Something": "abc",
     "Something": "abc",
     "Something": "abc",
     "Something": "abc",
     "Something": "abc"
}
PS: I do not want to use modules for JSON decoding.And sorry for my bad english

Thank you in advance
Ludak is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-16-2015 , 09:55   Re: JSON decoding
Reply With Quote #2

Quote:
PS: I do not want to use modules for JSON decoding
Why? That would an appropriate way.
__________________
Arkshine is offline
Ludak
Member
Join Date: Oct 2014
Old 06-16-2015 , 11:18   Re: JSON decoding
Reply With Quote #3

Quote:
Originally Posted by Arkshine View Post
Why? That would an appropriate way.
Because a lot of peole do not know how to install modules and do not even read install instructions at all
I tried that with module, a lot of people asked me to help them with instalation i do not have time to reply to everyone
Ludak is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-16-2015 , 12:04   Re: JSON decoding
Reply With Quote #4

What issue do you have with exolents include file?
__________________
Bugsy is offline
Ludak
Member
Join Date: Oct 2014
Old 06-16-2015 , 12:18   Re: JSON decoding
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
What issue do you have with exolents include file?
Code:
#include < amxmodx >
#include < json >

public plugin_init( ) {
	register_plugin( "Plugin", "1.0", "Author" );
	
	new JSON: jObject, iPosition, szText[ 2 ][ 32 ];
	new const szInput[ ] = "{^"Test1^": ^"hey^", ^"Test2^": ^"you^"}";
	
	jObject = json_decode( szInput, iPosition, strlen( szInput ) );
	json_get_string( jObject, "Test1", szText[ 0 ], charsmax( szText[ ] ) );
	json_get_string( jObject, "Test2", szText[ 1 ], charsmax( szText[ ] ) );
	
	log_to_file( "json_test.txt", "%s = %s", szText[ 0 ], szText[ 1 ] );
}
This returns: =
Only character = without text from json

Edit:
Code:
L 06/16/2015 - 18:24:39: Log file started (file "cstrike/addons/amxmodx/logs/json_test.txt") (game "cstrike") (amx "1.8.2")
L 06/16/2015 - 18:24:39: =

Last edited by Ludak; 06-16-2015 at 12:27.
Ludak is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-16-2015 , 12:26   Re: JSON decoding
Reply With Quote #6

I'll take a look at the code when I get on my home PC to find out why it is not working.
__________________
Bugsy is offline
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 06-16-2015 , 16:26   Re: JSON decoding
Reply With Quote #7

@Ludak:
PHP Code:
#include < amxmodx >

public plugin_init( ) {
    
register_plugin"Test JSON decoding""1.0""Milutinke (ByM)" );
    
    
register_concmd"TestJSON""Test" );
}

public 
Test( ) {
    new 
iFile fopen"json.txt""rt" );
    
ParseJSONiFile );
}

stock ParseJSONiFile ) {
    static 
szData128 ], szLeft64 ], szRight64 ];

    while( 
iFile && !feofiFile ) )  {
        
fgetsiFileszDatacharsmaxszData ) );
        
        
trimszData );
        
replace_allszDatacharsmaxszData ), "^"", "" );
        replace_all( szData, charsmax( szData ), "
,", "" );
            
        strtok( szData, szLeft, charsmax( szLeft ), szRight, charsmax( szRight ), ':' );
        
        if( szLeft[ 0 ] == '{' || szLeft[ 0 ] == '}' ) continue;

        server_print( "
%= %s", szLeft, szRight );
    }
    
    fclose( iFile );

Tested.

JSON:
Code:
{
     "a": 1,
     "b": "c",
     "c": "d"
}
Result:
Code:
a = 1
b = c
c = d
This code is working, although it has one floor
It can not parse json code that is in one line and it can not parse more complex json array it works just with this array that you have.
If you want you can use regex for determing specific elements with array in them.
If you are using external api from web site, you can use json_encode( $szInput, JSON_PRETTY_PRINT );

Quote:
Originally Posted by Bugsy View Post
I'll take a look at the code when I get on my home PC to find out why it is not working.
Can you fix that include it is not working for me to
I tried to fix it but with no sucess

Last edited by milutinke; 06-17-2015 at 02:12.
milutinke is offline
Send a message via Skype™ to milutinke
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:03.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode