[Tips] How to convert JSON to string array in C#

0
30

If you wanna to convert JSON to string array in C#, My below code is for you!

How to convert JSON to string array in C#

You should use 2 function:
– One is convertStringToObjectsJSon
– Another is IEmumerable to cast type
View below code:


// insert System.Web.Script.Serialization namespace
using System.Web.Script.Serialization;
public static object convertStringToObjectsJson(string str)
{
try
{
// string str = "{ 'context_name': { 'lower_bound': 'value', 'pper_bound':
'value', 'values': [ 'value1', 'valueN' ] } }";
JavaScriptSerializer j = new JavaScriptSerializer();
object json = j.Deserialize(str, typeof(object));

if (json == null)
return null;

return json;
}
catch
{
return null;
}
}

string[] arString = new string[] { };

string sResult = getResponeseString("username", "password");
jsonResult = clsJson.convertStringToObjectsJson(sResult);

// convert json to array String[]
arString =
((IEnumerable)jsonResult).Cast<object>().Select(x => x.ToString()).ToArray();

getResponeseString(username, password)

getResponseseString is use httpwebresquest with GET method. You can see here

If you wanna download my full source code, you can download from here

If you see this information is helpful for you, please share and +1 google plus for my channel
Thanks a lots!
Zidane