Click here to Skip to main content
15,507,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have json string want to return in array
{
  "_luserid": "",
  "_mypage": "Login",
  "_myip": "16.513,75.2975",
  "_divid": "nomobile",
  "_appver": "1.2",
  "_useragent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36",
  "_platform": "Win32",
  "_mymsg": "Login Page Load",
  "_mbrand": "undefined-undefined-undefined"
}


What I have tried:

[WebMethod]
public static string gappv_json(string json_obj) 
{
  User user = new User(json_obj); // here deta not return 
string mymsg= user._mymsg;
}
public class User
{
   public User(string jdata)
    {
     var json_serializer = new JavaScriptSerializer();
     jsonDATA json_list = json_serializer.Deserialize<jsonDATA>(jdata);
    }
}
public class jsonDATA
{
 public string _luserid { get; set; }
 public string _mymsg { get; set; }
 public string _mypage { get; set; }
 public string _myip { get; set; }
 public string _divid { get; set; }
 public string _appver { get; set; }
 public string _useragent { get; set; }
 public string _gps { get; set; }
 public string _platform { get; set; }
 public string _mbrand { get; set; }

}
Posted
Updated 30-Jun-22 0:46am

1 solution

Your JSON isn't an array of data - it's separate strings within a single class instance.
To use it as an array, you would have to create an array / List of strings, then add each property of your class to it:
C#
public class User
    {
    string[] data = null;
    public User(string jdata)
        {
        var json_serializer = new JavaScriptSerializer();
        jsonDATA json_list = json_serializer.Deserialize<jsonDATA>(jdata);
        List<string> strings = new List<string>();
        strings.Add(json_list._luserid);
        strings.Add(json_list._mymsg);
        strings.Add(json_list._mypage);
        ...
        data = strings.ToArray();
        }
    }
 
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