Home > Software engineering >  Consult about JSON objects in the array processing
Consult about JSON objects in the array processing

Time:09-15

To obtain the following JSON characters:
How to loop reads the data in the data, using MSScriptControl. ScriptControl, find some data are not possible, not to VB for a long time, don't know jar and no expert guidance, thank you
"Status" : 1,
"Info", "Get devices state history success",
"Data" : [
{
"1466744001" : 0
},
{
"1466736802" : 1
},
{
"1466736801" : 0
},
{
"1466730484" : 1
},
{
"1466729602" : 1
},
{
"1466729601" : 0
},
{
"1466723247" : 1
},
{
"1466720287" : 1
},
{
"1466720286" : 0
},
{
"1466715990" : 1
},
{
"1466715204" : 1
},
{
"1466715200" : 0
},
{
"1466708748" : 1
},
{

CodePudding user response:

Replace
The Split

CodePudding user response:

 Option Explicit 

Private Sub Command1_Click ()
Dim strJSON As String
Dim p1 & amp; , p2 As Long
Dim As Long I

'the original JSON text content
StrJSON="" "status ": 1," "& amp; VbCrLf & amp; _
"" "info "" :" "Get devices state history success", "" & amp; VbCrLf & amp; _
"" "data ":" [" & amp; vbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466744001 ", "0" & amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466736802 ":" 1 "& amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466736801 ", "0" & amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466730484 ":" 1 "& amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466729602 ":" 1 "& amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466729601 ", "0" & amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466723247 ":" 1 "& amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf
StrJSON=strJSON & amp; "" "1466720287 ":" 1 "& amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466720286 ", "0" & amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466715990 ":" 1 "& amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466715204 ":" 1 "& amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466715200 ", "0" & amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{" & amp; VbCrLf & amp; _
"" "1466708748 ":" 1 "& amp; VbCrLf & amp; _
"}, "& amp; VbCrLf & amp; _
"{"
'the data extraction:
P1=InStr (1, strJSON, "data" : "") 'to find the first key words: data" :
If (p1) Then
P1=InStr (6 & amp; + p1, strJSON, "" "") to find the first double quotation marks within the section "data"
I=0 'counter variable
Do 'loop to extract data
If (0=(p1) Then Exit the Do
P1=(p1 + 1
I=I + 1
The p2=InStr (p1, strJSON, "" "")
'output sequence number and data
The Debug. Print I, Mid $(strJSON, p1, p2, p1)
P1=InStr (1 + p2, strJSON, "" "")
Loop
End the If
End Sub
'output content:
1 1466744001
''2 1466736802
'3 1466736801
4 1466730484
'5 1466729602
'6 1466729601
'7 1466723247
''8 1466720287
9 1466720286
'10 1466715990
'11 1466715204
'12 1466715200
''13 1466708748

CodePudding user response:

Thank two upstairs, maybe I didn't say clear, now to show as follows:
I am using the Microsoft Script Control 1.0 controls,

 'add Microsoft Script Control 1.0 
Private Sub Command1_Click ()
'Dim JSON As Object
'Set the JSON=CreateObject (" MSScriptControl. ScriptControl)
'JSON. Language="JScript"
'JSON. AddCode "var JSON=" & amp; StrHTTP & amp; ";"
Dim strDlist () As String

With ScriptControl1
The Reset
. Language="javascript"
"AddCode var json=" & amp; Text1. Text & amp; ";"
StrDlist=Split (Eval (" json data "), ", ")
The Debug. Print. Eval (" json. The info ")
The Debug. Print. Eval (" json. The status ")
Debug. Print. Eval (". The json data ") 'array of objects, and how to iterate over the json. The data objects and attributes?????
End With
End Sub


The sample program can be downloaded here:
http://mndsoft.com/downfiles/jsont.rar


CodePudding user response:

Search a VBS code, don't know how to convert, have know friends can refer to,
Traverse the JSON data in the VBS arrays in
Now just know originally jscript with built-in enumerator!!!!!!
 var JSON={
"Parse" : function (jsonstring) {
Try {
Return (new Function (jsonstring "return" + + ";" ()));
} the catch (ex) {}
return null;
}
}
The function MyEnumerator (SRC) {return new Enumerator (SRC); }
The set d=JSON. Parse (" {" "a" ": [{" "a "" : 1}, {" a" ":" 2}, {" a "" :" 3}]} ")
The set e=MyEnumerator (da)
The do while not e.a tEnd ()
Response. Write e.i tem (). A & amp; "
E.M oveNext ()
Loop

The set d=JSON. Parse (" {" "a" ": [1, 2, 3]}")
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related