问题
Through LotusScript I am consuming a webpage that returns json values and I have been unable to find any library out there for lotusscript, other than ls.snapps.JSONReader from openntf. It works, but documentation is limited. I am having trouble reading a nested array in the value list. I was able to get it to work in java using the ibm.common.utils.... library, but was having trouble with mac client and another library (javax.swing.*) so I switched to LotusScript.
I am hoping someone else has experience with the ls.snapps.JSONReader library, or maybe a different idea on how to do this. Here is the sample:
to read it I use
Dim jsonReader As jsonReader
Dim vResults As Variant
Dim vPieces As Variant
Dim sJSON As string
sJson= |{ "colorsArray":[{
"red":"#f00",
"green":"#0f0",
"blue":"#00f",
"cyan":"#0ff",
"magenta":"#f0f",
"yellow":"#ff0",
"black":"#000"
}
]
}|
Set jsonReader = New JsonReader
Set vResults = jsonReader.parse(sJson)
vPieces = vResults.items
I have no trouble when I set a single level object such as:
sJSON = |{"a":"a4255524","a24":true,"ax":"WER!!","b":"Some text"}|
I use the getItemValue method
msgbox vResults.getitemValue("a24")
will return a 'true' value
Has anyone used this JSON parser and can you give me some advice on how to get the data out?
UPDATE and interim solution: To get json values I had to do one of two things:
use ls Replace functions to extract the single dimension data (ie, remove {"colorsArray":[ on the left side and ]} on the right side., then use snapps json reader.
I created a java library using the SBT JSON libs and called it from LotusScript. Paul Bastide put a good writeup on using the java lib http://bastide.org/2014/03/15/using-the-ibm-social-business-toolkit-to-process-json-objects/
Here is the java code:
import com.ibm.commons.util.io.json.JsonException;
import com.ibm.commons.util.io.json.JsonJavaFactory;
import com.ibm.commons.util.io.json.JsonJavaObject;
import com.ibm.commons.util.io.json.JsonParser;
import com.ibm.sbt.services.client.base.datahandlers.JsonDataHandler;
import lotus.domino.*;
public class GetJSON extends AgentBase {
public static String pJSON( String jData, String jEntry, String jValue ) {
String result2="";
try {
System.out.println("data: " + jData + "\n" + "\n" + "jEntry & jValue: " + jEntry + ", " + jValue);
// print to debug console
// System.out.println("jData: " + jData);
JsonJavaObject jsonObject = (JsonJavaObject) JsonParser.fromJson(JsonJavaFactory.instanceEx, jData );
JsonDataHandler handler = new JsonDataHandler();
handler.setData(jsonObject);
JsonJavaObject entryJson=handler.getEntry(jEntry);
result2=entryJson.getAsString(jValue);
} catch (Exception e) {
System.out.println("Error: " + e);
e.printStackTrace();
result2="";
}
return result2; }
}
and I call it from LotusScript as follows:
Option Public
Option Declare
Use "($getJson)"
UseLSX "*javacon"
Dim mySession As JavaSession
Dim myClass As JavaClass
Dim getJson As JavaObject
result = ""
'....
'add vars here
'....
Set mySession = New JavaSession()
Set myClass = mySession.GetClass("GetJSON")
Set GetJson = myClass.CreateObject()
MsgBox GetJson.pJSON( result2, "colorsArray", "red" )
IMPORTANT NOTE on the above, in the array string I had to remove the brackets [ and ] because I was getting an SBT array incompatibility error in java. I think by doing that it may have turned it into a single level object, but if you look at Paul's example from above URL, you'll see he doesn't add them to his example either.
I would rather do this in all Java or all LotusScript, and will probably use the modified json string with snapps, just looking for a better solution.
回答1:
Here is the working code for your JSON string. Try it.
Dim jsonReader As JSONReader
Dim vResults As Variant
Dim vPieces As Variant
Dim sJSON As String
sJson= |{ "colorsArray":[{
"red":"#f00",
"green":"#0f0",
"blue":"#00f",
"cyan":"#0ff",
"magenta":"#f0f",
"yellow":"#ff0",
"black":"#000"
}
]}|
Set jsonReader = New JSONReader
Set vResults = jsonReader.parse(sJson)
Set vResultData = vResults.GetItemValue("colorsArray")
Forall vResult In vResultData.Items
Msgbox Cstr(vResult.GetItemValue("red"))
Msgbox Cstr(vResult.GetItemValue("green"))
Msgbox Cstr(vResult.GetItemValue("blue"))
Msgbox Cstr(vResult.GetItemValue("cyan"))
Msgbox Cstr(vResult.GetItemValue("magenta"))
Msgbox Cstr(vResult.GetItemValue("yellow"))
Msgbox Cstr(vResult.GetItemValue("black"))
End Forall
来源:https://stackoverflow.com/questions/36820010/trying-to-get-lotusscript-json-reader