简体   繁体   中英

extracting key value pair

I am working on Rest api. It is working fine. When i request it, it sends me a response. Actually this response is an array consisting of sub array and further key:value pairs in them. Now i want to extract these key:value pairs in C#. I thought of regex to extract substring but i have no knowledge of regex yet. but i tried to first get the sub array like.

Input string is

[
  {
    "id": "1",
    "name": "kashif",
    "father_name": "mirza",
    "contact": "21321",
    "email": "sdf",
    "image": "sdfsdf"
  },
  {
    "id": "2",
    "name": "Kashif",
    "father_name": "Mirza",
    "contact": "0342809211",
    "email": "shadbagh@wele.com",
    "image": "photo[1].jpg"
  },
  {
    "id": "3",
    "name": "Abdussadiq",
    "father_name": "Abdul Haq Khan",
    "contact": "03449066113",
    "email": "abdus.sadiq04@gmail.com",
    "image": "1393602_698558840157121_1872079026_n.jpg"
  }
]

C# code for regex

 Match match = Regex.Match(input, @"(?<=\{)(.*)(?=\})",
        RegexOptions.IgnoreCase);
if (match.Success)
    {

        foreach(var matchgroup in match.Groups)
            Console.WriteLine(matchgroup);
    }

It returns me the result but not according to my needs. It returns me like

    "id": "1",
    "name": "kashif",
    "father_name": "mirza",
    "contact": "21321",
    "email": "sdf",
    "image": "sdfsdf"
  },
  {
    "id": "2",
    "name": "Kashif",
    "father_name": "Mirza",
    "contact": "0342809211",
    "email": "shadbagh@wele.com",
    "image": "photo[1].jpg"
  },
  {
    "id": "3",
    "name": "Abdussadiq",
    "father_name": "Abdul Haq Khan",
    "contact": "03449066113",
    "email": "abdus.sadiq04@gmail.com",
    "image": "1393602_698558840157121_1872079026_n.jpg"

So that means it is removing the the just outer brackets but not the inner ones.

So my questions are:

  1. What i am doing wrong in above code?
  2. If i have to get key:value pair then what else i need to do?

You don't have to use regex for that.
The data you're trying to parse are in JSON format.

For example, you could parse it with Json.NET

Dictionary<string, dynamic> MyData = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(JsonData);

If you want a native support, try JavascriptSerializer (it also gives you an example of how to parse it directly into classes, which is better than dictionary regarding on your JSON data structure)

Dictionary<string, dynamic> MyData = JavaScriptSerializer.Deserialize<Dictionary<string, dynamic>>(JsonData);

You are using greedy regex .* that's why you are getting all. Instead use non-greed .*? have then in pieces.

Match match = Regex.Match(input, @"(?<=\{)(.*?)(?=\})", RegexOptions.IgnoreCase);

After that, if you need to parse more, then use regex on the captured group( matchgroup ):

(".*?(?<!\\)":".*?(?<!\\)")    <-- you'll get spoofed here if single quote, or no quotes

But still , I'll always suggest you to use any json parsing library for this.

使用regexp进行这种任务是一种不好的做法使用Json.NET库

First you can create a class like this:

    public class MyClass
    {
       public int Id { get; set; }
       public string Name { get; set; }
       public string Father_Name { get; set; }
       public string Contact { get; set; }
       public string Email { get; set; }
       //Could be byte[]
       public string Image { get; set; }
    }

And then you can use the JavaScriptSerializer class like below:

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    List<MyClass> myClassCollection = serializer.Deserialize<List<MyClass>>(result);
    //Where result is your json string

Your API returns data in JSON format, you should either use .NET's built-in JavaScriptSerializer class under System.Web.Script.Serialization namespace or third party library such as Json.NET for reading this response.

To use JavaScriptSerializer , first you'll need to add System.Web.Extensions reference using Add Reference...

Using Regex to parse JSON is a bad practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM