0

How can I parse a json object with c # like below? So MessageBox.show (message);

So; MessageBox.Show(result[0].id);

`

 {
    "message":"Customer updated",
    "result":[
        1,
       [
          {
             "id":1,
             "customer_name":"Andrew",
             "customer_lastname":"freeman",
             "customer_identity":"12345678",
          }
       ]
    ]
 }

`

johnvayne
  • 79
  • 3
  • 8
  • 3
    Does this answer your question? [Read and parse a Json File in C#](https://stackoverflow.com/questions/13297563/read-and-parse-a-json-file-in-c-sharp) – Magnetron Mar 04 '21 at 12:24
  • Also [How can I parse JSON with C#?](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c), [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object/9326146#9326146), [Parse JSON in C#](https://stackoverflow.com/questions/1212344/parse-json-in-c-sharp) – Magnetron Mar 04 '21 at 12:26

2 Answers2

0

I am coding for you with explanations but I think this json data is wrong. Because some expression is wrote without any key in the result key.

Your question short answer is you should use any Json deserialize nuget package like a newtonsoft.

And You should create classes to correspond it.

I will write a example with explanation for you, if you tell me more detail.

*Sorry for grammer rules because I'm new to english

I just wrote the passage code for you below

.Net core > Program.cs

using Newtonsoft.Json; //You should install this package from nuget package manager
using System; 

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //your json data (I added new some keys)
            string jsonData = "{"
                                +"\"message\":\"Customer updated\","
                                +"\"result\":[{"
                                               + "\"resultid\": 1,"
                                               + "\"mock\":["
                                                            + "{"
                                                                + "\"id\":1,"
                                                                + "\"customer_name\":\"Andrew\","
                                                                + "\"customer_lastname\":\"freeman\","
                                                                + "\"customer_identity\":\"12345678\","
                                                            + "}"
                                                        + "]"
                                          + "}]"
                            + "}";

            //this code convert json to class
            var Data = JsonConvert.DeserializeObject<Data>(jsonData);

            //result
            Console.WriteLine(Data.result[0].resultid);
        }
    }
}

Data class and the others below

    class Data
    {
        public string message { get; set; }
        public List<Datum> result { get; set; }
    }

    class Datum
    {
        public int resultid { get; set; }
        public List<Mock> mock { get; set; }
    }

    class Mock
    {
        public string id { get; set; }

        public string customer_name { get; set; }

        public string customer_lastname { get; set; }

        public string customer_identity { get; set; }
    }
ilyas varol
  • 788
  • 3
  • 10
  • 25
0

You can install the Newtonsoft.Json Nuget package, then use it to access a JSON object like so:

using Newtonsoft.Json;

MessageBox.Show(myJsonObject["result"][0]["id"]);

Assuming your json object is stored in the myJsonObject variable.

To store the json in a JObject (or a class), you can serialise/deserialise it or create a JObject manually:

var myJObject = new JObject(
                                    new JProperty("message", "Customer updated"),
                                    new JProperty("result", 
                                        new JArray(
                                            new JObject(
                                                new JProperty("id", 1),
                                                new JProperty("customer_name", "Andrew"),
                                                new JProperty("customer_lastname", "freeman"),
                                                new JProperty("customer_identity", "12345678")
                                            )   
                                        )
                                    )
                                );

Though it looks like your JSON could be invalid - you have an array within your result array that doesn't have a property name.

You can store the result object directly within the first array, as I have done in my example above, and remove the 1 as you have this in the id field:

{
    "message":"Customer updated",
    "result":[
          {
             "id":1,
             "customer_name":"Andrew",
             "customer_lastname":"freeman",
             "customer_identity":"12345678",
          }
     ]
 }
to6y
  • 96
  • 1
  • 9