JSON and using Statement for VB.NET 2003

1,615 views
Skip to first unread message

Aigle Libre

unread,
Sep 8, 2010, 7:23:26 AM9/8/10
to Jayrock, aziz...@gmail.com
Hello

vb.net 2003 for a project, I need to parse a JSON file
do you have any example?

Can you give me the equivalent of this code in vb.net 1.1 (Visual
studio 2003) ? :

string jsonText = @"[""Europe"", ""Asia"", ""Australia"",
""Antarctica"",
""North America"", ""South America"", ""Africa""]";

using (JsonTextReader reader = new JsonTextReader(new
StringReader(jsonText)))
{
while (reader.Read())
{
if (reader.TokenClass == JsonTokenClass.String &&
reader.Text.StartsWith("A"))
{
Console.WriteLine(reader.Text);
}
}
}


thank you

Atif Aziz

unread,
Sep 9, 2010, 11:44:34 AM9/9/10
to Jayrock

Here you go (also available at http://gist.github.com/570612):
 
Imports System.IO
Imports Jayrock.Json
 
Module MainModule
 
  Sub Main()
 
    Dim jsonText As String = _

      "[""Europe"", ""Asia"", ""Australia"", " _
      & """Antarctica"", ""North America"", ""South America"", " _
      & """Africa""]"
 
    Dim reader As JsonTextReader = New JsonTextReader( _
      New StringReader(jsonText))

    Try
      While reader.Read()
        If reader.TokenClass.Equals(JsonTokenClass.String) _
           AndAlso reader.Text.StartsWith("A") Then
           Console.WriteLine(reader.Text)
        End If
      End While
    Finally
      reader.Close()
    End Try
 
  End Sub
 
End Module

Atif Aziz

unread,
Sep 9, 2010, 11:45:59 AM9/9/10
to jay...@googlegroups.com
but I can not find the methods to parse the text, as the case of xml (eg method: node ...)
I tried to convert JSON to XML "JsonConvert.impotr ()" without success.
the method is not recognized, knowing that I found in the literature
  
JsonConvert.Import, without any type specification, will find reasonable mappings for JSON values as shown below:
  • JSON "null" becomes a null reference
  • JSON "true" or "false" = System.Boolean
  • JSON Number = Jayrock.Json.JsonNumber
  • JSON String = System.String
  • JSON Array = System.Collections.IList
  • JSON Object = System.Collections.IDictionary
 
So there is *node* concept. When you import a JSON Array or Object value, just use it like a an IList or IDictionary as you would in VB 2003.
Jayrock also directly supports JSON to XML conversion via JsonML. See http://jsonml.org/ to understand the resulting XML and JsonMLCodec in Jayrock for more.
- Atif
On Thu, Sep 9, 2010 at 9:34 AM, nagib ziouar <nagib....@gmail.com> wrote:
Hello

Above all know that I'm French and I have trouble with English
Hello

Above all know that I'm French and I have trouble with English ;-)

I will come to you to explain my need for a project. NET 1.1

a Web service sends the JSON text
I have to parse the text to insert into the database
I make reference to the dll jayrock.Json
but I can not find the methods to parse the text, as the case of xml (eg method: node ...)
I tried to convert JSON to XML "JsonConvert.impotr ()" without success.
the method is not recognized, knowing that I found in the literature

is what you can give me explanation and examples for good progress in my project?

Thank you very much



Nagib ZIOUAR


2010/9/8 Atif Aziz <aziz...@gmail.com>

Here you go (also available at http://gist.github.com/570612):
 
Imports System.IO
Imports Jayrock.Json
 
Module MainModule
 
  Sub Main()
 
    Dim jsonText As String = _

      "[""Europe"", ""Asia"", ""Australia"", " _
      & """Antarctica"", ""North America"", ""South America"", " _
      & """Africa""]"
 
    Dim reader As JsonTextReader = New JsonTextReader( _
      New StringReader(jsonText))
    Try
      While reader.Read()
        If reader.TokenClass.Equals(JsonTokenClass.String) _
           AndAlso reader.Text.StartsWith("A") Then
           Console.WriteLine(reader.Text)
        End If
      End While
    Finally
      reader.Close()
    End Try
 
  End Sub
 
End Module


On Wed, Sep 8, 2010 at 1:23 PM, Aigle Libre <nagib....@gmail.com> wrote:




--
Cordialement.
Nagib ZIOUAR
Tel: 06 58 34 68 70

330.gif

Aigle Libre

unread,
Sep 14, 2010, 9:41:10 AM9/14/10
to Jayrock, Atif Aziz
Thank you very much Aziz

here is my JSON text :
----
{"Address":{"Address1":"rue Jules Verne","Address2":"Le
Brezet","Country":"France","County":"Puy de
Zôme","Region":"Auvergne","Town":"Clermont-
Ferrand","ZipCode":"63000","ExtendedProperties":[]},"Birthdate":"\/
Date(157762800000)\/","CandidateId":
5614651,"Email":"pierre...@societe.com","Firstname":"Pierre","MobilePhone":"06
12 34 56 78","Name":"Dupont","Phone":"01 23 45 67 89","Qualification":
{"Code":"232","Title":"Infirmier bloc opératoire","ExtendedProperties":
[]},"Sex":"M","Contacteur":false,"Emailing":true,"Nationality":
{"Code":"FR","Title":"Française","ExtendedProperties":
[]},"ExtendedProperties":[]},{"Address":{"Address1":"rue du
port","Address2":null,"Country":null,"County":null,"Region":null,"Town":"Marseille","ZipCode":"13000","ExtendedProperties":
[]},"Birthdate":"\/Date(150588000000)\/","CandidateId":
2432416,"Email":"Sophie...@gmail.com","Firstname":"Sophie","MobilePhone":"06
11 34 56 78","Name":"Michel","Phone":"01 22 45 67 89","Qualification":
{"Code":"128","Title":"Kiné","ExtendedProperties":
[]},"Sex":"F","Contacteur":true,"Emailing":false,"Nationality":null,"ExtendedProperties":
[]}
----

I want to parse this text and update the database.

the objective is to recover the value with the name of the column

Can you give me a demo on this example please



On 9 sep, 17:45, Atif Aziz <aziza...@gmail.com> wrote:
> > but I can not find the methods to parse the text, as the case of xml (eg
> > method: node ...)
> > I tried to convert JSON to XML "JsonConvert.impotr ()" without success.
> > the method is not recognized, knowing that I found in the literature
>
> JsonConvert.Import, without any type specification, will find reasonable
> mappings for JSON values as shown below:
>
>    - JSON "null" becomes a null reference
>    - JSON "true" or "false" = System.Boolean
>    - JSON Number = Jayrock.Json.JsonNumber
>    - JSON String = System.String
>    - JSON Array = System.Collections.IList
>    - JSON Object = System.Collections.IDictionary
>
> So there is *node* concept. When you import a JSON Array or Object value,
> just use it like a an IList or IDictionary as you would in VB 2003.
> Jayrock also directly supports JSON to XML conversion via JsonML. Seehttp://jsonml.org/to understand the resulting XML and JsonMLCodec in
> Jayrock for more.
> - Atif
>
> On Thu, Sep 9, 2010 at 9:34 AM, nagib ziouar <nagib.zio...@gmail.com> wrote:
> > Hello
>
> > Above all know that I'm French and I have trouble with English [?]
> > Hello
>
> > Above all know that I'm French and I have trouble with English ;-)
>
> > I will come to you to explain my need for a project. NET 1.1
>
> > a Web service sends the JSON text
> > I have to parse the text to insert into the database
> > I make reference to the dll jayrock.Json
> > but I can not find the methods to parse the text, as the case of xml (eg
> > method: node ...)
> > I tried to convert JSON to XML "JsonConvert.impotr ()" without success.
> > the method is not recognized, knowing that I found in the literature
>
> > is what you can give me explanation and examples for good progress in my
> > project?
>
> > Thank you very much
>
> > Nagib ZIOUAR
>
> > 2010/9/8 Atif Aziz <aziza...@gmail.com>
>
> >  Here you go (also available athttp://gist.github.com/570612):
>
> >> Imports System.IO
> >> Imports Jayrock.Json
>
> >> Module MainModule
>
> >>   Sub Main()
>
> >>     Dim jsonText As String = _
>
> >>       "[""Europe"", ""Asia"", ""Australia"", " _
> >>       & """Antarctica"", ""North America"", ""South America"", " _
> >>       & """Africa""]"
>
> >>     Dim reader As JsonTextReader = New JsonTextReader( _
> >>       New StringReader(jsonText))
> >>     Try
> >>       While reader.Read()
> >>         If reader.TokenClass.Equals(JsonTokenClass.String) _
> >>            AndAlso reader.Text.StartsWith("A") Then
> >>            Console.WriteLine(reader.Text)
> >>         End If
> >>       End While
> >>     Finally
> >>       reader.Close()
> >>     End Try
>
> >>   End Sub
>
> >> End Module
>
> >>   On Wed, Sep 8, 2010 at 1:23 PM, Aigle Libre <nagib.zio...@gmail.com>wrote:
>
> >>> Hello
>
> >>> vb.net 2003 for a project, I need to parse a JSON file
> >>> do you have any example?
>
> >>> Can you give me the equivalent of this code in vb.net 1.1 (Visual
> >>> studio 2003) ? :
>
> >>> string jsonText = @"[""Europe"", ""Asia"", ""Australia"",
> >>> ""Antarctica"",
> >>>  ""North America"", ""South America"", ""Africa""]";
>
> >>> using (JsonTextReader reader = new JsonTextReader(new
> >>>  StringReader(jsonText)))
> >>> {
> >>>    while (reader.Read())
> >>>    {
> >>>        if (reader.TokenClass == JsonTokenClass.String &&
> >>>            reader.Text.StartsWith("A"))
> >>>        {
> >>>            Console.WriteLine(reader.Text);
> >>>        }
> >>>    }
> >>> }
>
> >>> thank you
>
> > --
> > Cordialement.
> > Nagib ZIOUAR
> > Tel: 06 58 34 68 70
>
>
>
>  330.gif
> < 1 000AfficherTélécharger

Atif Aziz

unread,
Sep 17, 2010, 2:58:17 AM9/17/10
to Jayrock
This should get you started (including conversion of specially encoded date strings into DateTime objects), assuming your JSON data is in a file called json.txt:
 
Dim jsonText As String = File.ReadAllText("json.txt")
Dim imported As Object = JsonConvert.Import(jsonText)
Dim obj As IDictionary = DirectCast(imported, IDictionary)
For Each e In obj
    Dim value As Object = e.Value
    If TypeOf value Is String Then
        Dim match As Match = Regex.Match(value.ToString(), _
                                 "\A\/Date\(([0-9]+)\)\/\z")
        If match.Success Then
            Dim ticks As Long = Long.Parse(match.Groups(1).Value)
            value = New DateTime(ticks)
        End If
    End If
    Console.WriteLine("{0}: {1}", e.Key, value)
Next

- Atif

Reply all
Reply to author
Forward
0 new messages