Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to get result_code from this array ?

"Array\n(\n    [result] => false\n    [result_code] => TS-1027\n    [result_message] => There is no balance in the account\n)\n"


What I have tried:

JObject details = JObject.Parse(result);
string Code = (string)details["result_code"];


but this error appear Unexpected character encountered while parsing value: A. Path '', line 0, position 0
Posted
Updated 11-Aug-20 4:58am
Comments
Richard MacCutchan 11-Aug-20 9:42am    
As Chris C already explained, that is not JSON. You need to talk to the people who provide it to you.

That text isn't valid JSON output, that instead looks like the output from printing a PHP array. If you're encoding the array you should consider using the PHP json_encode[^] function instead which would produce a valid output similar to:
{ "result": false, "result_code": "TS-1027", "result_message": ".." }
 
Share this answer
 
Comments
Golden Basim 11-Aug-20 9:36am    
no, I'm not the code developer. i just get this result from the SMS service provider. so i should deal with this result anyway.
Chris Copeland 11-Aug-20 9:49am    
That's unfortunate. Without being able to manipulate the output, I don't think there's any standard way in C# to decode a PHP array output to an object.

In this case you would probably need to either: a) manually parse the response character by character to extract the values you need; b) use regular expressions to extract the information from the text; c) use regular expressions to reformat the response into a valid JSON format and then attempt to deserialize it.

An example regular expression might be something like \[result\]\s+?\=\>\s+?(true|false)
That isn't JSON, or even close to: you will have to either write your own parser for it or talk to whoever produced it and find out exactly what data specification should are working with (in fact, you pretty much want to do that anyway - you'll probably need it in order to write your own parser).
A JSON array of those would look like this:
{Array:[{"result":"false", "result_code":"TS-1027", "result_message":"There is no balance in the account"}]}

And would work with these classes:
C#
public class Array
{
    public string result { get; set; }
    public string result_code { get; set; }
    public string result_message { get; set; }
}

public class Example
{
    public IList<Array> Array { get; set; }
}

Have a look at these:
Create C# classes from JSON[^]
And
Create JSON from C# classes[^]
 
Share this answer
 
Comments
Richard MacCutchan 11-Aug-20 11:34am    
It looks like the result of printing an array in PHP.
OriginalGriff 11-Aug-20 11:54am    
Could be - but what does it generate when the array contains more than one element? :laugh:
Richard MacCutchan 11-Aug-20 12:31pm    
Something like:
Array
(
    [0] => Array
        (
            [result] =>
            [result_code] => TS-1027
            [result_message] => There is no balance in the account
        )

    [1] => Array
        (
            [result] => 1
            [result_code] => TS-1099
            [result_message] => The balance in the account is £12.96
        )

)
You could use regex

C#
string text = @"Array\n(\n    [result] => false\n    [result_code] => TS-1027\n    [result_message] => There is no balance in the account\n)\n";
var matches = Regex.Matches(text, @"\[(\w*)\]\s=>\s([^\\]*)");

foreach (Match m in matches)
{
    string name = m.Groups[1].Value;
    string value = m.Groups[2].Value;

    System.Diagnostics.Debug.WriteLine(name + " = " + value);
}


result

result = false
result_code = TS-1027
result_message = There is no balance in the account
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900