Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. A liitle help on submitting a json array, no-primitive to a web service

A liitle help on submitting a json array, no-primitive to a web service

Scheduled Pinned Locked Moved ASP.NET
htmldata-structuresjsonhelptutorial
12 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jkirkerx

    This is the first time that I am submitting a array of data to a web service, sort of in reverse. So a web service can only accept primitive values, I get it. I want to pick up a json array of values, but not sure how to go about it. So here's the layout. JSON input

    {
    "template_Name": "tshirts",
    "template_Array": [
    {
    "name": "Red",
    "code": "RED"
    },
    {
    "name": "Green",
    "code": "GRN"
    },
    {
    "name": "Blue",
    "code": "BLU"
    },
    {
    "name": "Black",
    "code": "BLK"
    },
    {
    "name": "White",
    "code": "WHT"
    }
    ]
    }

    And the Function declaration of the Web Service

    _
    Public Function create_DynamicCart_Template(
    ByVal template_Name As String,
    ByVal template_Array() As String) As String

        Dim dwExitCode As Integer = 2
        Dim json\_response As StringBuilder = New StringBuilder
        Dim html\_Stream As String = Nothing
    

    So because the template_Array() contains the array marker, it's not a primitive. I read that I have to consume the web service in another application, or perhaps I just take out the array () chars, and swallow it as a char array. Maybe make a class that represents the array, template_Array As TA, Any suggestions?,

    C Offline
    C Offline
    CodeHawkz
    wrote on last edited by
    #3

    I dealt with this problem just yesterday. I found a really really convenient way to that. But I am horrible with VB.Net otherwise I could have helped you out. Do you think if I post the C# code, you would be able to figure the VB.Net version of it? Btw, my methods only works with .Net Framework 3.0 and above. Is that what you use? If it's 4.0 I can give you a very easy method :)

    J 1 Reply Last reply
    0
    • C CodeHawkz

      I dealt with this problem just yesterday. I found a really really convenient way to that. But I am horrible with VB.Net otherwise I could have helped you out. Do you think if I post the C# code, you would be able to figure the VB.Net version of it? Btw, my methods only works with .Net Framework 3.0 and above. Is that what you use? If it's 4.0 I can give you a very easy method :)

      J Offline
      J Offline
      jkirkerx
      wrote on last edited by
      #4

      Sure, I can translate with no problem I did it working last night, I was surprised at what the fix was, and was able to generate my xml file. Would like to see what you came up with, because I think I'm going to use more of this in the future, it's pretty cool.

      C 1 Reply Last reply
      0
      • J jkirkerx

        Sure, I can translate with no problem I did it working last night, I was surprised at what the fix was, and was able to generate my xml file. Would like to see what you came up with, because I think I'm going to use more of this in the future, it's pretty cool.

        C Offline
        C Offline
        CodeHawkz
        wrote on last edited by
        #5

        Hey there, Glad it worked out. Post your method here as well, so I can see whether I can improve my solution :) ExpandoObject Documentation[^] JSON Library[^] that I used

        // This only works in .NET 4.0
        dynamic result = new ExpandoObject();
        IDictionary resultData = (IDictionary)result;

        resultData.add("templateName", "tshirts");
        var templateArray = new [] {
        new { Name = "Red", Code = "RED" },
        new { Name = "Green", Code = "GRN" },
        new { Name = "Blue", Code = "BLU" }
        };

        resultData.add("templateArray", templateArray);

        string jsonString = JsonConvert.SerializeObject(result);

        This will output a JSON string like the one you initially mentioned. I think the code is self explanatory. Feel free to reach out for further clarifications. Hope this helps, cheers

        J 1 Reply Last reply
        0
        • C CodeHawkz

          Hey there, Glad it worked out. Post your method here as well, so I can see whether I can improve my solution :) ExpandoObject Documentation[^] JSON Library[^] that I used

          // This only works in .NET 4.0
          dynamic result = new ExpandoObject();
          IDictionary resultData = (IDictionary)result;

          resultData.add("templateName", "tshirts");
          var templateArray = new [] {
          new { Name = "Red", Code = "RED" },
          new { Name = "Green", Code = "GRN" },
          new { Name = "Blue", Code = "BLU" }
          };

          resultData.add("templateArray", templateArray);

          string jsonString = JsonConvert.SerializeObject(result);

          This will output a JSON string like the one you initially mentioned. I think the code is self explanatory. Feel free to reach out for further clarifications. Hope this helps, cheers

          J Offline
          J Offline
          jkirkerx
          wrote on last edited by
          #6

          Oh, that's pretty slick. I built my json the old fashioned way using string builder. I just caught the net4.0 comment, using 2.0 on this project. I was just fine tuning my function to correct the first record error being sort of blank.

          _
          Public Function create_DynamicCart_Template(
          ByVal template_Name As String,
          ByVal template_Array() As templateElements) As String

              Dim dwExitCode As Integer = 2 'An unknown error has occured
              Dim json\_response As StringBuilder = New StringBuilder
          
              Try
                  Dim bTemplates As Boolean = DoesFoldersExist("~/App\_Data/Products/dynamicItems/templates")
                  Dim bDisplay As Boolean = DoesFoldersExist("~/App\_Data/Products/pageDisplay")
          
                  'Create the First Record
                  Dim new\_Template As dynamicItem3.template = New dynamicItem3.template
                  Dim new\_Element1 As dynamicItem3.templateElement\_1 = New dynamicItem3.templateElement\_1
          
                  new\_Element1.Name = template\_Array(0).Name
                  new\_Element1.suffix = template\_Array(0).Code
          
                  If (template\_Array(0).Type.ToUpper = "SIMPLE") Then
                      new\_Element1.elementType = dynamicItem3.templateElement\_1ElementType.Simple
                  Else
                      new\_Element1.elementType = dynamicItem3.templateElement\_1ElementType.Complex
                  End If
          
                  Dim new\_Element1\_Record() As dynamicItem3.templateElement\_1 = {new\_Element1}
                  Dim new\_Element1\_Record\_Count As Integer = 0
          
                  'Now Build the rest of the records
                  For i As Integer = 1 To template\_Array.Length - 1
                      'Build the XML Array from the JSON Array
                      Array.Resize(new\_Element1\_Record, new\_Element1\_Record.Length + 1)
                      new\_Element1\_Record(new\_Element1\_Record.Length - 1) = New dynamicItem3.templateElement\_1
                      new\_Element1\_Record(new\_Element1\_Record.Length - 1).Name = template\_Array(i).Name
                      new\_Element1\_Record(new\_Element1\_Record.Length - 1).suffix = template\_Array(i).Code
          
                      If (template\_Array(i).Type.ToUpper = "SIMPLE") Then
                          new\_Element1\_Record(new\_Element1\_Record.Length - 1).elementType = dynamicItem3.templateElement\_1ElementType.Simple
                      Else
                          new\_Element1\_Record(new\_Element1\_Record.Length - 1).elementType = dynamicItem3.templateElement\_1ElementType.Complex
                      End If
          
                      new\_Element1\_Record\_Count += 1
          
          C 1 Reply Last reply
          0
          • J jkirkerx

            Oh, that's pretty slick. I built my json the old fashioned way using string builder. I just caught the net4.0 comment, using 2.0 on this project. I was just fine tuning my function to correct the first record error being sort of blank.

            _
            Public Function create_DynamicCart_Template(
            ByVal template_Name As String,
            ByVal template_Array() As templateElements) As String

                Dim dwExitCode As Integer = 2 'An unknown error has occured
                Dim json\_response As StringBuilder = New StringBuilder
            
                Try
                    Dim bTemplates As Boolean = DoesFoldersExist("~/App\_Data/Products/dynamicItems/templates")
                    Dim bDisplay As Boolean = DoesFoldersExist("~/App\_Data/Products/pageDisplay")
            
                    'Create the First Record
                    Dim new\_Template As dynamicItem3.template = New dynamicItem3.template
                    Dim new\_Element1 As dynamicItem3.templateElement\_1 = New dynamicItem3.templateElement\_1
            
                    new\_Element1.Name = template\_Array(0).Name
                    new\_Element1.suffix = template\_Array(0).Code
            
                    If (template\_Array(0).Type.ToUpper = "SIMPLE") Then
                        new\_Element1.elementType = dynamicItem3.templateElement\_1ElementType.Simple
                    Else
                        new\_Element1.elementType = dynamicItem3.templateElement\_1ElementType.Complex
                    End If
            
                    Dim new\_Element1\_Record() As dynamicItem3.templateElement\_1 = {new\_Element1}
                    Dim new\_Element1\_Record\_Count As Integer = 0
            
                    'Now Build the rest of the records
                    For i As Integer = 1 To template\_Array.Length - 1
                        'Build the XML Array from the JSON Array
                        Array.Resize(new\_Element1\_Record, new\_Element1\_Record.Length + 1)
                        new\_Element1\_Record(new\_Element1\_Record.Length - 1) = New dynamicItem3.templateElement\_1
                        new\_Element1\_Record(new\_Element1\_Record.Length - 1).Name = template\_Array(i).Name
                        new\_Element1\_Record(new\_Element1\_Record.Length - 1).suffix = template\_Array(i).Code
            
                        If (template\_Array(i).Type.ToUpper = "SIMPLE") Then
                            new\_Element1\_Record(new\_Element1\_Record.Length - 1).elementType = dynamicItem3.templateElement\_1ElementType.Simple
                        Else
                            new\_Element1\_Record(new\_Element1\_Record.Length - 1).elementType = dynamicItem3.templateElement\_1ElementType.Complex
                        End If
            
                        new\_Element1\_Record\_Count += 1
            
            C Offline
            C Offline
            CodeHawkz
            wrote on last edited by
            #7

            Woahh.. That's one long solution :omg: I think you can simplify that solution by compiling the logic into one class using a dictionary object or a stringbuilder object internally to hold the data. I will post a sample, if I get time around one of these days.

            J 1 Reply Last reply
            0
            • C CodeHawkz

              Woahh.. That's one long solution :omg: I think you can simplify that solution by compiling the logic into one class using a dictionary object or a stringbuilder object internally to hold the data. I will post a sample, if I get time around one of these days.

              J Offline
              J Offline
              jkirkerx
              wrote on last edited by
              #8

              I thought it was pretty optimized, considering it building a XML file, and writing it to disk.

              C 1 Reply Last reply
              0
              • J jkirkerx

                I thought it was pretty optimized, considering it building a XML file, and writing it to disk.

                C Offline
                C Offline
                CodeHawkz
                wrote on last edited by
                #9

                Hey there, I am sorry about the really late reply. But I've been working with web technologies and XML manipulation much. One thing I've learnt is that XML is great for data transferring but it's slow and heavy. For instance, if you want to send one piece of data that is huge XML is fine. But if you want to send lots of small pieces of data, the number of tags generated for each small object, adds an unnecessary weight. I hope this gives you the basic idea of what I mean. I won't have time to write a fully generalized JSON class :( But it should be really easy to write a logic to generalize your specific class without using XML. Let me know if you still want it. Basic idea for it would be to implement an interface on all the classes you want to JSONify (I made that word up :P) and pass it on to a special class. The implementation of the interface would return a JSON data string for that specific object. The special class would bind them all to one piece of data. Hope this helps, Regards

                J 1 Reply Last reply
                0
                • C CodeHawkz

                  Hey there, I am sorry about the really late reply. But I've been working with web technologies and XML manipulation much. One thing I've learnt is that XML is great for data transferring but it's slow and heavy. For instance, if you want to send one piece of data that is huge XML is fine. But if you want to send lots of small pieces of data, the number of tags generated for each small object, adds an unnecessary weight. I hope this gives you the basic idea of what I mean. I won't have time to write a fully generalized JSON class :( But it should be really easy to write a logic to generalize your specific class without using XML. Let me know if you still want it. Basic idea for it would be to implement an interface on all the classes you want to JSONify (I made that word up :P) and pass it on to a special class. The implementation of the interface would return a JSON data string for that specific object. The special class would bind them all to one piece of data. Hope this helps, Regards

                  J Offline
                  J Offline
                  jkirkerx
                  wrote on last edited by
                  #10

                  That's OK on the late reply, it's a discussion, and I got it working using a class with get and set to accept a JSON array of items. So I understand how to pass non-primitives to a web service now. I find XML to be really fast. I use XML reader and writer, and a class file to code against. So basically each xml element is like an array, that can be resized with Array.resize. And yes it does help solidify the techniques needed to accomplish the task. Thanks for collaborating with me on the subject. Guess not that many people are at that level yet.

                  C 1 Reply Last reply
                  0
                  • J jkirkerx

                    That's OK on the late reply, it's a discussion, and I got it working using a class with get and set to accept a JSON array of items. So I understand how to pass non-primitives to a web service now. I find XML to be really fast. I use XML reader and writer, and a class file to code against. So basically each xml element is like an array, that can be resized with Array.resize. And yes it does help solidify the techniques needed to accomplish the task. Thanks for collaborating with me on the subject. Guess not that many people are at that level yet.

                    C Offline
                    C Offline
                    CodeHawkz
                    wrote on last edited by
                    #11

                    hehe :) It's my pleasure to have a good discussion and help someone out. XML is not slow for your eye, but it's really slow when it comes to traversing the nodes (i.e. compared to others of course). Anyways, am sure for your purpose it won't effect much. However I'll try to write a sample code that would perform json serialization easily, over the weekend, and share it. regards

                    J 1 Reply Last reply
                    0
                    • C CodeHawkz

                      hehe :) It's my pleasure to have a good discussion and help someone out. XML is not slow for your eye, but it's really slow when it comes to traversing the nodes (i.e. compared to others of course). Anyways, am sure for your purpose it won't effect much. However I'll try to write a sample code that would perform json serialization easily, over the weekend, and share it. regards

                      J Offline
                      J Offline
                      jkirkerx
                      wrote on last edited by
                      #12

                      It's a holiday weekend here, labor day, but I may send another array to a web service next week. I'm reworking my shopping cart and checkout program, implementing better and faster code, while cleaning up. Have a great weekend!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups