0

i try to slit a text and put it into a dictionary , the problem i my text doesn't have a a clear structure : text :

{
   "about": "where I'm meant to be...",
   "bio": "Visit my official blog at:\n\nhttp://ABC.com/  \n\nAdd me on Twitter:\n\nhttp://www.ABC.com/ABC",
   "category": "Public figure",
   "is_published": true,
   "location": {
      "street": "",
      "city": "Los Angeles",
      "state": "CA",
      "country": "United States",
      "zip": ""
   },
   "talking_about_count": 254637,
   "username": "ABC",
   "website": "http://kimkardashian.celebuzz.com/\nhttp://www.twitter.com/kimkardashian\n",
   "were_here_count": 0,
   "id": "114696805612",
   "name": "ABC",
   "link": "http://www.ABC.com/ABC",
   "likes": 0,
   "cover": {
      "cover_id": "000000000",
      "source": "http://ABC.jpg",
      "offset_y": 0,
      "offset_x": 200
   }
}

As you see i have the "," as a delimiter , the problem is that there some composed objects like the :

"location": {
      "street": "",
      "city": "Los Angeles",
      "state": "CA",
      "country": "United States",
      "zip": ""
   },

that's why I can't use the string.Split(' '); i heard about the regular expressions but I don't know how to use them Is there any solution to get those information separated into a dictionary or any other structure

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1863359
  • 317
  • 1
  • 10
  • 23

2 Answers2

1

Your data is in a standard format (JSON) and there are parsers already written for it. You can download Json.NET easy through NuGet in Visual Studio.

Regular expressions are a powerful tool that makes pattern matching a lot simpler. For me that's as far as they go. They can be used to create parsers and all sorts of other things, but it's complicated.

So you could create your own JSON parser using regular expressions, but it'll take a lot of time. It would be like building a lockpick when there is a key available.

Weyland Yutani
  • 4,682
  • 1
  • 22
  • 28
0

JavaScriptSerializer may satisfy your needs

using System.Web.Script.Serialization;
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string,string>>(jsonText);
Console.WriteLine(dict["some_number"]);

See: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

Nikita
  • 422
  • 5
  • 14