1

I have JSON data.:

{"title":"",
"query":"pakistan",
"for":"daily",
"method":3,
"prayer_method_name":"University Of Islamic Sciences,
 Karachi (Hanafi)",
"daylight":"0",
"timezone":"5",
"map_image":"http:\/\/maps.google.com\/maps\/api\/staticmap?center=30.375321,69.345116&sensor=false&zoom=13&size=300x300",
"sealevel":"1376",
"today_weather":{"pressure":null,"temperature":null},
"link":"http:\/\/muslimsalat.com\/pakistan",
"qibla_direction":"258.33",
"latitude":"30.375321",
"longitude":"69.345116",
"address":"","city":"",
"state":"",
"postal_code":"",
"country":"Pakistan",
"country_code":"PK",
"items":[{"date_for":"2016-6-9","fajr":"3:43 am","shurooq":"5:09 am","dhuhr":"12:21 pm","asr":"5:15 pm","maghrib":"7:34 pm","isha":"9:00 pm"}],
"status_valid":1,
"status_code":1,
"status_description":"Success."}

I only need specifice data from the JSON, for example,

"fajr":"3:43 am", "shurooq":"5:09 am", "dhuhr":"12:21 pm", "asr":"5:15 pm", "maghrib":"7:34 pm", "isha":"9:00

How can I achieve that?

2
  • What have you already tried? What about that approach is troubling you? Commented Jun 9, 2016 at 5:35
  • You aren't asking a question, so we can't give you an answer... Please elaborate. Commented Jun 9, 2016 at 5:36

4 Answers 4

1

You can use a JSON serializer such as http://www.newtonsoft.com/json.

Then you'd have the following model:

public class Item
{
    public string date_for { get; set; }
    public string fajr { get; set; }
    public string shurooq { get; set; }
    public string dhuhr { get; set; }
    public string asr { get; set; }
    public string maghrib { get; set; }
    public string isha { get; set; }
}

public class ItemContainer
{
    public List<Item> Items { get; set; }
}

and using json.net you could retrieve the values

var data = JsonConvert.DeserializeObject<ItemContainer>(your json);
if (data.Items.Count > 0)
{
   var fajr = data.Items[0].fajr;
   var dhuhr = data.Items[0].dhuhr; 
   ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

let say your json was stored on MyData variable:

so to take the values are:

  • MyData.items => return object
  • MyData.items.fajr => return string
  • MyData.items.shurooq => return string
  • MyData.items.dhuhr => return string
  • MyData.items.asr => return string
  • MyData.items.maghrib => return string
  • MyData.items.isha => return string

Using newtonsoft.json:

var shalaSchedule = JsonConvert.DeserializeObject<Dictionary<string, object>>(MyData);

so you can read the data from shalaSchedule["items"][0]["magrib"].toString();

Comments

0

You have to parse your JSON into an object. For that you can use JSON.NET.

Here is an example on how to parse a JSON string into a dynamic object:

string source = "YOUR JSON";
dynamic data = JObject.Parse(source);
Console.WriteLine(data.fajr);
Console.WriteLine(data.shurooq);
Console.WriteLine(data.dhuhr);
etc...

Comments

0

You can create a class which contains attribute like json keys.Then you can easily deserialize it as below.

class TemplateResponse
{
    public String multicast_id;
    public String success;
    public String failure;
    public String canonical_ids;
    public Result[] results;

    public class Result
    {
        public String message_id;
        public String registration_id;
        public String error;
    };
}

Json String:

"\"multicast_id\":7400896764380883211,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1351777805148960%39895cf0f9fd7ecd\"}]}"

Then deserialize like below snippet:

TemplateResponse result = new JavaScriptSerializer().Deserialize<TemplateResponse>(json);
string status = result.success;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.