locked
Json retrieve data RRS feed

  • Question

  • User932259438 posted

    Hi,

     var details = JObject.Parse("{ 'data': [{'AuthorUserID': 'dasda','Replies': 'test'},{'AuthorUserID': 'da31sda','Replies': 'tes765t'}]}");
    
    
            Button1.Text = (string)details["data"][0]["Replies"];

    I want to get like that: dasda, test </br> da31sda, Repplies
    How can I do this. I tried with [0] but returns only one values.

    If I have unlimited I do not want use [0], .... I need to retrieve a data from json like I sent. How can I do this?


    Wednesday, January 3, 2018 8:58 PM

Answers

  • User2103319870 posted

    I want to get like that: dasda, test </br> da31sda, Repplies
    How can I do this. I tried with [0] but returns only one values.

    You can try with the below code. 

    Add below namespace to your page

    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json;

    Create a class in your application

            [JsonObject(MemberSerialization.OptIn)]
            public class YourClassName
            {
                [JsonProperty("AuthorUserID")]
                public string AuthorUserID { get; set; }
    
                [JsonProperty("Replies")]
                public string Replies { get; set; }
            }

    You can serialize json data to list like below

     JToken root = JObject.Parse("{ 'data': [{'AuthorUserID': 'dasda','Replies': 'test'},{'AuthorUserID': 'da31sda','Replies': 'tes765t'}]}");
                JToken user = root["data"];
                //Deserialize the Json to list
                var deserializedJsonDatalist = JsonConvert.DeserializeObject<List<YourClassName>>(user.ToString());
                string result = "";
                //Loop through values 
                foreach (YourClassName p in deserializedJsonDatalist)
                {
                    //Your code to process the values
                    result += p.AuthorUserID + "," + p.Replies + "<br/>"
                }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 3, 2018 9:21 PM

All replies

  • User475983607 posted

    Hi,

     var details = JObject.Parse("{ 'data': [{'AuthorUserID': 'dasda','Replies': 'test'},{'AuthorUserID': 'da31sda','Replies': 'tes765t'}]}");
    
    
            Button1.Text = (string)details["data"][0]["Replies"];

    I want to get like that: dasda, test </br> da31sda, Repplies
    How can I do this. I tried with [0] but returns only one values.

    If I have unlimited I do not want use [0], .... I need to retrieve a data from json like I sent. How can I do this?

    The posted snippet is an example of a JSON array syntax therefore the index is required in order to select an object from the array.  If you want to iterate over every object in the array then use a for...loop.

    At this point your question is not clear.  Can you explain the problem you are trying to solve and provide source code that reproduces the issue?

    Wednesday, January 3, 2018 9:15 PM
  • User2103319870 posted

    I want to get like that: dasda, test </br> da31sda, Repplies
    How can I do this. I tried with [0] but returns only one values.

    You can try with the below code. 

    Add below namespace to your page

    using Newtonsoft.Json.Linq;
    using Newtonsoft.Json;

    Create a class in your application

            [JsonObject(MemberSerialization.OptIn)]
            public class YourClassName
            {
                [JsonProperty("AuthorUserID")]
                public string AuthorUserID { get; set; }
    
                [JsonProperty("Replies")]
                public string Replies { get; set; }
            }

    You can serialize json data to list like below

     JToken root = JObject.Parse("{ 'data': [{'AuthorUserID': 'dasda','Replies': 'test'},{'AuthorUserID': 'da31sda','Replies': 'tes765t'}]}");
                JToken user = root["data"];
                //Deserialize the Json to list
                var deserializedJsonDatalist = JsonConvert.DeserializeObject<List<YourClassName>>(user.ToString());
                string result = "";
                //Loop through values 
                foreach (YourClassName p in deserializedJsonDatalist)
                {
                    //Your code to process the values
                    result += p.AuthorUserID + "," + p.Replies + "<br/>"
                }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 3, 2018 9:21 PM