GVKun编程网logo

在Windows上的Excel VBA中,如何为已解析的JSON变量获取字符串化的JSON表示而不是“[object Object]”?

17

针对在Windows上的ExcelVBA中,如何为已解析的JSON变量获取字符串化的JSON表示而不是“[objectObject]”?这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Andr

针对在Windows上的Excel VBA中,如何为已解析的JSON变量获取字符串化的JSON表示而不是“[object Object]”?这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Android Volley POST JsonObject并获取JsonArray或JsonObject或其他响应、Android中的JSONObject和JSONArray解析json数据、fastjson的jsonobject.parseObject(jsonstr)是忽略指定字段或大文本处理、Handlebars.js解析对象而不是[Object object]等相关知识,希望可以帮助到你。

本文目录一览:

在Windows上的Excel VBA中,如何为已解析的JSON变量获取字符串化的JSON表示而不是“[object Object]”?

在Windows上的Excel VBA中,如何为已解析的JSON变量获取字符串化的JSON表示而不是“[object Object]”?

在这里回答我自己的问题.
我已经在Excel VBA中使用 JSON做了一些工作,并发布了大量的发现,我将在Q&格式
https://stackoverflow.com/help/self-answer http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/

所以在stackoverflow的其他地方,人们可以看到有关在VBA中解析JSON的问题,但他们似乎错过了一两个技巧.

首先,我重新使用自定义JSON解析库,而是使用ScriptControl的Eval方法作为我所有JSON代码的基础.
此外,我们还表达了本机Microsoft解决方案的偏好.

这是问题In Excel VBA on Windows,how to mitigate issue of dot syntax traversal of parsed JSON broken by IDE’s capitalisation behaviour?的先验问题.它显示了如何使用VBA.CallByName更健壮
而不是使用点语法来遍历解析的JSON对象.另一个先前的问题In Excel VBA on Windows,how to loop through a JSON array parsed?也示出了它也是如何
用于访问数组元素.但是CallByName返回一个好奇的变量类型,它在Watch窗口中显示为Object / JScriptTypeInfo
如果在即时窗口中键入一个Debug.Print(或将鼠标悬停在变量上),则会得到无信息的“[object Object]”.

我们如何改进并获得JSON字符串表示?

以下是您在Debug.Print(?)之后在立即窗口中看到的内容以及将鼠标悬停在变量上的屏幕截图.

object Object

这是5系列的问题3.这是完整系列

Q1 In Excel VBA on Windows,how to mitigate issue of dot syntax traversal of parsed JSON broken by IDE’s capitalisation behaviour?

Q2 In Excel VBA on Windows,how to loop through a JSON array parsed?

Q3 In Excel VBA on Windows,how to get stringified JSON respresentation instead of “[object Object]” for parsed JSON variables?

Q4 In Windows Excel VBA,how to get JSON keys to pre-empt “Run-time error ‘438’: Object doesn’t support this property or method”?

Q5 In Excel VBA on Windows,for parsed JSON variables what is this JScriptTypeInfo anyway?

解决方法

与使用解析的JSON对象相关的其他堆栈溢出问题的答案使用迷你脚本方法,我们可以在此处使用此方法.

首先,我们承认Douglas Crockford是“Javascript:The Good Parts”(http://shop.oreilly.com/product/9780596517748.do)的作者
并且是javascript专家.所以我们很乐意在字符串化方面采用他的代码.我们可以使用简单的Xml HTTP请求获取他的代码
(通常缩写为XHR)并将返回结果传递给ScriptControl的AddCode方法.然后添加一些允许我们覆盖默认表示的代码
通过调用Douglas的库来获取“[object Object]”.然后确保我们动态地将覆盖添加到我们所有的JScriptTypeInfo变量,
我们用DecodeJsonString()包装的ScriptControl的Eval方法
以及我们用GetJSONObject()包装的VBA.CallByName.

从而,

'Tools->References->
'Microsoft Script Control 1.0;  {0E59F1D2-1FBE-11D0-8FF2-00A0D10038BC}; C:\Windows\SysWOW64\msscript.ocx
'Microsoft Xml,v6.0

Option Explicit

Private Function GetScriptEngine() As ScriptControl
    Static soScriptEngine As ScriptControl
    If soScriptEngine Is nothing Then
        Set soScriptEngine = New ScriptControl
        soScriptEngine.Language = "JScript"

        soScriptEngine.AddCode GetJavaScriptLibrary("https://raw.githubusercontent.com/douglascrockford/JSON-js/master/json2.js")
        soScriptEngine.AddCode "function overridetoString(jsonObj) { jsonObj.toString = function() { return JSON.stringify(this); } }"
    End If
    Set GetScriptEngine = soScriptEngine
End Function

Private Function GetJavaScriptLibrary(ByVal sURL As String) As String

    Dim xHTTPRequest As MSXML2.XMLHTTP60
    Set xHTTPRequest = New MSXML2.XMLHTTP60
    xHTTPRequest.Open "GET",sURL,False
    xHTTPRequest.send
    GetJavaScriptLibrary = xHTTPRequest.responseText

End Function

Private Function DecodeJsonString(ByVal JsonString As String) As Object
    Dim oScriptEngine As ScriptControl
    Set oScriptEngine = GetScriptEngine

    Set DecodeJsonString = oScriptEngine.Eval("(" + JsonString + ")")

    Call oScriptEngine.Run("overridetoString",DecodeJsonString) '* this gives JSON rendering instead of "[object Object]"

End Function

Private Function GetJSONObject(ByVal obj As Object,ByVal sKey As String) As Object
    Dim objReturn As Object
    Set objReturn = VBA.CallByName(obj,sKey,VbGet)
    Call GetScriptEngine.Run("overridetoString",objReturn) '* this gives JSON rendering instead of "[object Object]"
    Set GetJSONObject = objReturn
End Function

Private Sub TestJSONParsingWithCallByName2()

    Dim sJsonString As String
    sJsonString = "{'key1': 'value1','key2': { 'key3': 'value3' } }"


    Dim objJSON As Object
    Set objJSON = DecodeJsonString(sJsonString)

    Stop


    Dim objKey2 As Object
    Set objKey2 = GetJSONObject(objJSON,"key2")
    Debug.Print objKey2
    Stop

End Sub

下面是一个新代码的屏幕截图,其中显示了JScriptTypeInfo变量的字符串化

enter image description here

Android Volley POST JsonObject并获取JsonArray或JsonObject或其他响应

Android Volley POST JsonObject并获取JsonArray或JsonObject或其他响应

在齐射中,我们具有从服务器检索数据的能力,例如jsonObject,jsonArray和String.在以下示例中,我们可以从服务器获取jsonObject或jsonArray响应,

public static void POST(HashMap<String, String> params, final Listeners.ServerResponseListener listener) {
    JsonObjectRequest req1 = new JsonObjectRequest(ApplicationController.URL, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("Response:", response.toString());
                    if (listener != null)
                        listener.onResultJsonObject(response);
                    else
                        Log.e(TAG,"Error: SetServerResponse interface not set");
                }
            }, new Response.ErrorListener() {
        @Override
        public void one rrorResponse(VolleyError error) {
            Log.e("Error: ", error.getMessage());
        }
    });
    ApplicationController.getInstance().addToRequestQueue(req1);
}

我的问题是我想从此方法发送jsonObject并从服务器获取jsonArray或jsonObject,而我无法使用此方法简单地从服务器获取数组.例如,我必须对此jsonObject进行过滤服务器响应:

HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "AbCdEfGh123456");
params.put("search_count", "10");
params.put("order_by", "id");

服务器返回jsonArray并且我无法通过Volley响应得到它

解决方法:

查看JsonArrayRequest的源代码.有一个构造函数,它接收JSONObject.你应该检查一下

Android中的JSONObject和JSONArray解析json数据

Android中的JSONObject和JSONArray解析json数据

今天介绍一下关于json数据解析,我们使用Android中的JSONObject和JSONArray解析json数据,有android开发的朋友可以参考一下。

       String strJson = "{"students":[{"name":"Jack","age":12}, {"name":"Vista","age":23}, {"name":"Kaka","age":22}, {"name":"Hony","age":31}]}";
        try {
            JSONObject jo = new JSONObject(strJson);
            JSONArray jsonArray = (JSONArray) jo.get("students");
            for (int i = 0; i                 JSONObject o = (JSONObject) jsonArray.get(i);
                System.out.println("name:" + o.getString("name") + "," + "age:"
                        + o.getInt("age"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

 

2.使用gson中的JsonReader解析json数据

try {
            String string = "{"class":1, "students":[{"name":"jack", "age":21},{"name":"kaka", "age":21},{"name":"lucy", "age":21}]}";
            StringReader sr = new StringReader(string);
            JsonReader jr = new JsonReader(sr);
            jr.beginObject();
            if (jr.nextName().contains("class")) {
                System.out.println("班级: " + jr.nextString());
                if (jr.nextName().equals("students")) {
                    jr.beginArray();
                    while (jr.hasNext()) {
                        jr.beginObject();
                        if (jr.nextName().equals("name"))
                            System.out.print("姓名:" + jr.nextString());
                        if (jr.nextName().equals("age")) {
                            System.out.println(" , 年龄:" + jr.nextInt());
                        }
                        jr.endObject();
                    }
                    jr.endArray();
                }
            }
            jr.endObject();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


Json解析库gson: http://code.google.com/p/google-gson/

fastjson的jsonobject.parseObject(jsonstr)是忽略指定字段或大文本处理

fastjson的jsonobject.parseObject(jsonstr)是忽略指定字段或大文本处理

各位大神好,最近在做微信开发,这是微信的返回数据格式,获取到数据后想转换成jsonobject进行处理,在jsonObject.parseObject(jsonStr)时,item下的content过长导致超过指定字数之后后面字就不显示了有没有什么办法可以,忽略该字段不让它输出,或者把所有数据都显示出来?

Handlebars.js解析对象而不是[Object object]

Handlebars.js解析对象而不是[Object object]

我正在使用Handlebars模板,并且[Object
object]中已经表示了JSON数据,如何在Handlebars之外解析此数据?例如,我试图通过handlebars标签在页面上填充一个JavaScript变量,但这不起作用。

有什么建议?谢谢!

编辑:

为了澄清起见,我使用的是带有手把的ExpressJS模板。在我的路线中,我有这个:

var user = {}
user = {'id' : 123,'name' : 'First Name'}

res.render('index',{user : user});

然后在index.hbs模板中,我有了一个{{user}}对象。我可以使用{{#each}}它遍历对象。但是,我也在使用Backbonejs,我想将此数据传递给View,例如:

myView = new myView({user : {{user}});

问题是,如果我将其放在console.log中,{{user}}只会[Object object]在源代码中显示,但会出现错误“意外的标识符”。

今天的关于在Windows上的Excel VBA中,如何为已解析的JSON变量获取字符串化的JSON表示而不是“[object Object]”?的分享已经结束,谢谢您的关注,如果想了解更多关于Android Volley POST JsonObject并获取JsonArray或JsonObject或其他响应、Android中的JSONObject和JSONArray解析json数据、fastjson的jsonobject.parseObject(jsonstr)是忽略指定字段或大文本处理、Handlebars.js解析对象而不是[Object object]的相关知识,请在本站进行查询。

本文标签: