-5

I would like to get single data from a string can any one help me to split the ID content alone. My Code that stores the Result value:

private void sendPostCompleted(object sender, UploadStringCompletedEventArgs e)
{
    try
    {
        MessageBox.Show("Success..");
        MessageBox.Show(e.Result);
        string res=(string)e.Result;//here res contains my data..
        //need to get ID value alone.
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
faroke moahmed
  • 313
  • 1
  • 5
  • 19
  • 1
    looks like you are getting a JSON string back. why don't you desieralize it to a class? it would be much easier than string parsing which is prone to errors etc... alternatively, do a split on the string by a common denominator and then find the id you are after, which appears to be on the last element - 2nd last - which will be the case when you split the string.... – Ahmed ilyas Dec 05 '13 at 09:33
  • I'm new to WP8 if you provide me some example it would be helpful.. – faroke moahmed Dec 05 '13 at 09:36
  • 1
    check out this [to autocreate your class for Json](http://json2csharp.com/) copy paste your json string (modify values if its private data). Then take a look here how to [how-to-parse-json-in-c/serialize](http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c) – Koryu Dec 05 '13 at 09:43
  • Deserializing JSON is not specific to WP8. There are plenty of resources (including tutorials) around which will help you along. Try looking up JSON.Net (from Newtonsoft) – flipchart Dec 05 '13 at 09:43

4 Answers4

7

Use json2csharp to create a C# class for your JSON-string. Simplified example from your JSON:

public class RootObject
{
    public string name { get; set; }
    public string description { get; set; }
    public string timezone { get; set; }
    public string id { get; set; }
}
//You can make the id of type int if you want

Fastest way for working with JSON is using Json.NET. Download the zip and use the dll from folder Portable40, this is the version that is compatible with Windows Phone 7. Reference the dll in your project and then your code should look similar to this:

string jsonString = "{\"name\":\"Qwer\", \"description\":\"\", \"timezone\":\"UTC\", \"id\":\"2912\"}";
var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonString);
int id = Convert.ToInt32(obj.id);
//value of id: 2912
Abbas
  • 14,186
  • 6
  • 41
  • 72
1

If you don't want a class to deserialize into you could do something like this:

JsonObject jsonObject = new JsonObject(e.Result);
string res= jsonObject["id"];
Iain
  • 2,500
  • 1
  • 20
  • 24
1

I solved my problem guys.. here is my sample code.. it works wel..

dynamic stuff = JsonConvert.DeserializeObject(res);

int id=stuff.id;
faroke moahmed
  • 313
  • 1
  • 5
  • 19
0

You should probably deserialize this into an object and then retrieve your values.

First create a class, with all your fields as parameters.

    class ResponseObject{
      public string Name{get;set;}
      public string Description{get;set;}
      ...
    }

and so on.

Then you can deserialize this response,

ResponseObject response = new JavaScriptSerializer().Deserialize<ResponseObject>(res);
Nanda
  • 1,038
  • 2
  • 16
  • 33
  • 1
    A quick way to make your C# class is (in >=VS2012) `Edit>Paste Special>Paste JSON as classes`... obviously you need to copy it first. – dav_i Dec 05 '13 at 09:46