-4

I have a json string which contains key/value pairs. Now, I want to convert the key/value pairs into DataTable with Columns (key,value)

Following is the json string:

{  
   "wifi_result":"1",
   "mic_result":"1",
   "video_result":"1",
   "touch_result":"1",
   "proximity_result":"1",
   "vibrator_result":"1",
   "power_key":"2",
   "accelerometer":"0",
   "earphone":"1",
   "memory_result":"1",
   "memory_internalSD":"1",
   "memory_internalSDSize":"25.0GB",
   "memory_externalSD":"0",
   "memory_externalSDSize":"",
   "memory_internalflash":"1",
   "memory_internalflashSize":"2.0GB",
   "memory_ram":"1",
   "memory_ramsize":"2.0GB",
   "lcd_result":"1",
   "lcd_broken":"1",
   "key_result":"1",
   "vol_key_up":"1",
   "vol_key_down":"0",
   "menu_key":"1",
   "headset_result":"1",
   "headset_leftearphone":"1",
   "headset_rightearphone":"0",
   "camera_result":"1",
   "camera_cameracount":"1",
   "camera_frontcam":"0",
   "camera_backcam":"0",
   "battery_result":"1",
   "battery_type":"1",
   "battery_level":"83",
   "battery_status":"1",
   "battery_voltage":"4204",
   "battery_temperature":"310",
   "battery_accharging":"0",
   "battery_usbcharging":"1",
   "audio_result":"1",
   "home_key":"1",
   "back_key":"1"
}

Required Output:

DataTable rows must be :

Key          |  Value
wifi_result  |    1
mic_result   |    1

Help appreciated!

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
  • possible duplicate of [How to convert json into datatable?](http://stackoverflow.com/questions/7641004/how-to-convert-json-into-datatable) – Tharif Jul 27 '15 at 07:40
  • hi @utility its not the duplicate you can see that its a plain json. Not the Array. – SHEKHAR SHETE Jul 27 '15 at 12:31
  • Whats wrong with this question? don't knw why people go on down voting atleast they would have been wrote a comment as the reason or leave the thread if don't know any solution :( – SHEKHAR SHETE Jul 27 '15 at 12:32
  • may be because there are lot of questions related already asked at stackoverflow and your question dont make any significant point or issue than others.. – Tharif Jul 27 '15 at 12:36

2 Answers2

3

You don't need a DataTable for this. In fact, it is a Dictionary<string,string>

Using Json.Net

var  dict = JsonConvert.DeserializeObject<Dictionary<string,string>>(json);

Now you have the key/value pairs.

If DataTable is a must, then

var dt = JArray.FromObject(dict.Select(x => x)).ToObject<DataTable>();
Eser
  • 12,346
  • 1
  • 22
  • 32
0

You can first deserializing into a c# class, and then turning it into a datatable.

OR

It is possible to go directly to a datatable, with JSON.NET Ex-:

DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
too_cool
  • 1,164
  • 8
  • 24