locked
how can i split this string() RRS feed

  • Question

  • User944339287 posted

    Hi,

    My web application getting this response message. How do i split it? I wanna get [id], [url] this two value. TQ

    {
      "id": "8X0Iyzaw",
      "collection_id": "inbmmepb",
      "paid": false,
      "state": "due",
      "amount": 200 ,
      "paid_amount": 0,
      "due_at": "2015-3-9",
      "email" :"api@billplz.com",
      "mobile": null,
      "name": "MICHAEL API V3",
      "url": "https://www.billplz.com/bills/8X0Iyzaw",
      "reference_1_label": "Reference 1",
      "reference_1": null,
      "reference_2_label": "Reference 2",
      "reference_2": null,
      "redirect_url": null,
      "callback_url": "http://example.com/webhook/",
      "description": "Maecenas eu placerat ante."
    }


    Wednesday, February 7, 2018 4:11 AM

All replies

  • User-37275327 posted

    I guess you can use Newtonsoft Json library, see below,

    https://www.newtonsoft.com/json/help/html/ReadJsonWithJsonTextReader.htm

    Wednesday, February 7, 2018 5:18 AM
  • User-707554951 posted

    Hi kengkit:

    Working code as below:

    using Newtonsoft.Json;
    protected void Page_Load(object sender, EventArgs e)
            {
                string jsonData = @"{
      'id': '8X0Iyzaw',
      'collection_id': 'inbmmepb',
      'paid': false,
      'state': 'due',
      'amount': 200 ,
      'paid_amount': 0,
      'due_at': '2015-3-9',
      'email' :'api@billplz.com',
      'mobile': null,
      'name': 'MICHAEL API V3',
      'url': 'https://www.billplz.com/bills/8X0Iyzaw',
      'reference_1_label': 'Reference 1',
      'reference_1': null,
      'reference_2_label': 'Reference 2',
      'reference_2': null,
      'redirect_url': null,
      'callback_url': 'http://example.com/webhook/',
      'description': 'Maecenas eu placerat ante.'
    }
    ";
                var myDetails = JsonConvert.DeserializeObject<RootObject>(jsonData);
                Response.Write(myDetails.id + "*" + myDetails.url);
            }
    public class RootObject
    {
    public string id { get; set; }
    public string collection_id { get; set; }
    public bool paid { get; set; }
    public string state { get; set; }
    public int amount { get; set; }
    public int paid_amount { get; set; }
    public string due_at { get; set; }
    public string email { get; set; }
    public object mobile { get; set; }
    public string name { get; set; }
    public string url { get; set; }
    public string reference_1_label { get; set; }
    public object reference_1 { get; set; }
    public string reference_2_label { get; set; }
    public object reference_2 { get; set; }
    public object redirect_url { get; set; }
    public string callback_url { get; set; }
    public string description { get; set; }
    }

    Output:

    Best regards

    Cathy

    Thursday, February 8, 2018 2:30 AM
  • User-2054057000 posted

    You can use .split() function like this:

    string idVal = yourJson.Split(',')[0].Split(':')[1]; // will give "8X0Iyzaw"
    string idVal1 = idVal.Trim().Substring(1, idVal.Length - 3); // will give 8X0Iyzaw

    Thursday, February 8, 2018 5:03 AM