开发者

convert vb.net data to json string and send it to a specific URL

this is the JSON string the data is required in to be sent using a given URL.

$jsonstr = '{"data":
     [{
      "id":"5",
      "owner_id":"0",
      "status":"unassigned",
      "first_name":"Test",
      "last_name":"IS",
      "tobacco_user":"",
      "date_of_birth":"",
      "age":"",
      "gender":"",
      "email":"lb@you.com",
      "zip":"",
      "phone开发者_如何学C":"(210)629-2560",
      "phone_type":"cell",
      "phone_alt":"",
      "phone_alt_type":"",
      "product_msip":"",
      "product_pdp":"",
      "product_sdhv":""
        },

I am using VB.net and i need to create this string using VB.net. I tried using namevaluecollection and doing a POST. I also tried making a string and send data using GET. Both failed. how can i do this?


Create an object with property names that are identical to those in your example, use the DataContract and DataMember attributes to mark serialization.

Then use the JavaScriptSerializer to serialize the object into JSON.

you can use the class when you want to work with JavaScript Object Notation (JSON) in managed code.


If you don't want to build an actual class as @Oded recommended you can just hack it together as a string. I usually use a NameValueCollection as you said you tried.

    ''//Setup some values
    Dim NVC As New NameValueCollection()
    NVC.Add("id", "5")
    NVC.Add("owner_id", "0")
    NVC.Add("status", "unassigned")

    ''//Convert to string
    Dim Pairs As New List(Of String)
    For Each N As String In NVC.Keys
        Pairs.Add(String.Format("""{0}"":""{1}""", N.Replace("""", "\"""), NVC(N).Replace("""", "\""")))
    Next
    Dim S = Join(Pairs.ToArray(), ",")

S now holds "id":"5","owner_id":"0","status":"unassigned" which you should be able to concat into your bigger JSON string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜