Author Topic: LispFunction JSON get values  (Read 373 times)

0 Members and 1 Guest are viewing this topic.

FELIX

  • Bull Frog
  • Posts: 205
LispFunction JSON get values
« on: April 22, 2021, 11:50:12 PM »
Code: [Select]
Dim JSLISTA = JsonConvert.DeserializeObject(JSON)
        For Each LL As String In JSLISTA
            'LL = {"id":9,"nserie0":"X9999","uname":"XXXXXX","cname":"YYYYYYY"}
            'ERROR = Conversion of type 'JObject' to type 'String' is not valid
            MsgBox(LL("id") & " - " & LL("nserie0") & " - " & LL("uname") & " - " & LL("cname")) '<<<<<<<<<<
        Next
OK.

n.yuan

  • Bull Frog
  • Posts: 311
Re: LispFunction JSON get values
« Reply #1 on: April 23, 2021, 08:45:44 AM »
Code: [Select]
Dim JSLISTA = JsonConvert.DeserializeObject(JSON)
        For Each LL As String In JSLISTA
            'LL = {"id":9,"nserie0":"X9999","uname":"XXXXXX","cname":"YYYYYYY"}
            'ERROR = Conversion of type 'JObject' to type 'String' is not valid
            MsgBox(LL("id") & " - " & LL("nserie0") & " - " & LL("uname") & " - " & LL("cname")) '<<<<<<<<<<
        Next

You need define a Type/Class that matches the JSON data structure: something like (sorry, code in C#):

public class MyObject
{
  public int id { set; get; }
  public string nserie0 { set; get; }
  public string uname { set; get; }
  public string cname { set; get; }
}

when deserializing JSON string, you do

var jsList = JsonConvert.DeserializeObject<MyObject>(jsonString);
foreach (var item in jsList)
{
  MessageBox.Show($"id: {item.id} - nserie0: {item.nserie0} - uname: {item.uname} - cname: {item.cname}");
}



FELIX

  • Bull Frog
  • Posts: 205
Re: LispFunction JSON get values
« Reply #2 on: April 23, 2021, 12:22:44 PM »
n.yuan your code didn't work because JSLISTA is a JSON ARRAY but from your code I managed to solve it.
Thank you so much.

Code: [Select]
Dim JSLISTA = JsonConvert.DeserializeObject(JSON) 'JSON / JSLISTA >>> ARRAY <<<
For Each JSL As Linq.JObject In JSLISTA
      MessageBox.Show($"id: {JSL.Item("id")} - nserie0: {JSL.Item("nserie0")} - uname: {JSL.Item("uname")} - cname: {JSL.Item("cname")}")
Next
OK.