以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  求助:如何解决微信公众号API中POST提交后的反馈JOSN数据的解析!谢谢  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=91478)

--  作者:李孝春
--  发布时间:2016/10/11 20:26:00
--  求助:如何解决微信公众号API中POST提交后的反馈JOSN数据的解析!谢谢
求助:如何解决微信公众号API中POST提交后的反馈JOSN数据的解析!谢谢

Dim postdata As String ="{""type"":""news"",""offset"":""0"",""count"":""2""}"
Dim dr As DataRow =DataTables("ACCESS_TOKEN").Find("公众号名称 = \'黔源科技\'")
Dim url = String.Format("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" & dr("ACCESSTOKEN") & "")
Using ms As New System.IO.MemoryStream()
Dim bytes = ConvertHelper.EncodingToBytes(postdata, System.Text.Encoding.UTF8)
ms.Write(bytes, 0, bytes.Length)
ms.Seek(0, System.IO.SeekOrigin.Begin)
Dim jsonString = Functions.Execute("HttpPost",url, ms) \'通过POST向接口传输菜单数据,并取得返回结果
\'output.show(jsonString )
End Using

上述代码可以实现当前图文信息的获取了 但是怎么实现将反馈得到的JSON数据解析出来呢?


官方提示:永久图文消息素材列表的响应如下:

{
   "total_count": TOTAL_COUNT,
   "item_count": ITEM_COUNT,
   "item": [{
       "media_id": MEDIA_ID,
       "content": {
           "news_item": [{
               "title": TITLE,
               "thumb_media_id": THUMB_MEDIA_ID,
               "thumb_url": THUMB_URL,
               "show_cover_pic": SHOW_COVER_PIC(0 / 1),
               "author": AUTHOR,
               "digest": DIGEST,
               "content": CONTENT,
               "url": URL,
               "content_source_url": CONTETN_SOURCE_URL
           },
           //多图文消息会在此处有多篇文章
           ]
        },
        "update_time": UPDATE_TIME
    },
    //可能有多个图文消息item结构
  ]
}
返回的值我需要得到title 、 author 、 url  、update_time这四个值  怎么实现操作呢?




--  作者:李孝春
--  发布时间:2016/10/11 20:27:00
--  回复:(李孝春)求助:如何解决微信公众号API中POST提...
下面的代码是实现GET方式得到的JSON文件解析,经操作是可以实现解析的:但是对应POST的我不熟悉操作,烦请大家多多指导下!谢谢
Dim XMLH As Object
XMLH = CreateObject("Microsoft.XMLHTTP")
Dim drs As List(Of DataRow) = DataTables("ACCESS_TOKEN").Select("AppID应用ID is not null")
For Each dr As DataRow In drs
XMLH.open("GET", "https://api.weixin.qq.com/datacube/getusershare?access_token=" & dr("ACCESSTOKEN") & "", True)   \'获得实时信息
XMLH.send(Nothing)
Do While XMLH.readyState <> 4
Application.DoEvents
Loop
\'
\'把数据json数据转化成对象
Dim json As String = XMLH.responseText
Dim ScriptControl As Object, data  As Object, JscriptCode As String
JscriptCode = "function toObject(json) {eval(""var o=""+json);return o;}"
ScriptControl = CreateObject("MSScriptControl.ScriptControl")
With ScriptControl
.Language = "Javascript"
.Timeout = -1
.AddCode(JscriptCode)
data = .Run("toObject", json)
End With
MessageBox.Show(json)
dr("voice_count") = data.voice_count
MessageBox.Show(dr("video_count"))
Next


看看上面的代码怎么变通下实现呢? 谢谢啦

--  作者:有点蓝
--  发布时间:2016/10/11 20:37:00
--  
Dim postdata As String ="{""type"":""news"",""offset"":""0"",""count"":""2""}"
Dim dr As DataRow =DataTables("ACCESS_TOKEN").Find("公众号名称 = \'黔源科技\'")
Dim url = String.Format("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" & dr("ACCESSTOKEN") & "")
Using ms As New System.IO.MemoryStream()
Dim bytes = ConvertHelper.EncodingToBytes(postdata, System.Text.Encoding.UTF8)
ms.Write(bytes, 0, bytes.Length)
ms.Seek(0, System.IO.SeekOrigin.Begin)
Dim jsonString = Functions.Execute("HttpPost",url, ms) \'通过POST向接口传输菜单数据,并取得返回结果
Dim json As String = jsonString 
Dim ScriptControl As Object, data  As Object, JscriptCode As String
JscriptCode = "function toObject(json) {eval(""var o=""+json);return o;}"
ScriptControl = CreateObject("MSScriptControl.ScriptControl")
With ScriptControl
.Language = "Javascript"
.Timeout = -1
.AddCode(JscriptCode)
data = .Run("toObject", json)
End With
MessageBox.Show(json)
dr("voice_count") = data.voice_count
MessageBox.Show(dr("video_count"))
Next
End Using

--  作者:李孝春
--  发布时间:2016/10/11 20:52:00
--  回复:(有点蓝)Dim postdata As String ="{""type""...
有点蓝老师:你楼上的代码我测试了一下,和我下面仿写的代码运行时候出现的错误是一致的,
错误如图:

代码如下:
Dim dr As DataRow = DataTables("ACCESS_TOKEN").Find("公众号名称 = \'黔源科技\'")
Dim XMLH As Object
XMLH = CreateObject("Microsoft.XMLHTTP")
XMLH.open("post", "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" & dr("ACCESSTOKEN") & "", True)   \'获得实时信息
XMLH.send("{""type"":""news"",""offset"":""0"",""count"":""2""}")
Do While XMLH.readyState <> 4
    Application.DoEvents
Loop

Dim json As String = XMLH.responseText
Dim ScriptControl As Object, data  As Object, JscriptCode As String
JscriptCode = "function toObject(json) {eval(""var o=""+json);return o;}"
ScriptControl = CreateObject("MSScriptControl.ScriptControl")
With ScriptControl
    .Language = "Javascript"
    .Timeout = -1
    .AddCode(JscriptCode)
    data = .Run("toObject", json)
End With
\'MessageBox.Show(json)
dr("title") = data.title
MessageBox.Show(dr("title"))


--  作者:客人
--  发布时间:2016/10/11 20:56:00
--  有点蓝老师 你帮助解决的代码及我仿写的代码 运行错误如下:
有点蓝老师  你帮助解决的代码及我仿写的代码  运行错误如下:

图片点击可在新窗口打开查看此主题相关图片如下:11.png
图片点击可在新窗口打开查看

运行的结果都是一样的


--  作者:李孝春
--  发布时间:2016/10/11 20:57:00
--  回复:(有点蓝)Dim postdata As String ="{""type""...
有点蓝老师  你帮助解决的代码及我仿写的代码  运行错误如下:

图片点击可在新窗口打开查看此主题相关图片如下:11.png
图片点击可在新窗口打开查看

运行的结果都是一样的

--  作者:有点蓝
--  发布时间:2016/10/11 21:05:00
--  
注意json的层次

dr("title") = data.item(0).content.news_item(0).title

--  作者:李孝春
--  发布时间:2016/10/11 21:08:00
--  回复:(有点蓝)注意json的层次dr("title") = data.i...
附带access_token值 你帮我看看  谢谢 



Dim postdata As String ="{""type"":""news"",""offset"":""0"",""count"":""2""}"
\'Dim dr As DataRow =DataTables("ACCESS_TOKEN").Find("公众号名称 = \'黔源科技\'")
\'Dim url = String.Format("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=" & dr("ACCESSTOKEN") & "")
Dim url = String.Format("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=SfjQStF9Bn3sULgsYc7aSMitqCbkkmcSCdFbMP9HAcVaQhyPz0TqkfnI0hzOXbFUrT_HPx9su5hDG4dAD4Vil0fDi3991gjtoDbDxgPukg3xhMPYtS5f_0BrAtVDnRIiRBWdAHAKUP")
Using ms As New System.IO.MemoryStream()
Dim bytes = ConvertHelper.EncodingToBytes(postdata, System.Text.Encoding.UTF8)
ms.Write(bytes, 0, bytes.Length)
ms.Seek(0, System.IO.SeekOrigin.Begin)
Dim jsonString = Functions.Execute("HttpPost",url, ms) \'通过POST向接口传输菜单数据,并取得返回结果
Dim json As String = jsonString 
Dim ScriptControl As Object, data  As Object, JscriptCode As String
JscriptCode = "function toObject(json) {eval(""var o=""+json);return o;}"
ScriptControl = CreateObject("MSScriptControl.ScriptControl")
With ScriptControl
.Language = "Javascript"
.Timeout = -1
.AddCode(JscriptCode)
data = .Run("toObject", json)
End With
\'MessageBox.Show(json)
Dim a As String =data.item(0).content.news_item(0).title
MessageBox.Show("a")
End Using


运行提示错误
图片点击可在新窗口打开查看此主题相关图片如下:22.png
图片点击可在新窗口打开查看
[此贴子已经被作者于2016/10/11 21:11:55编辑过]

--  作者:李孝春
--  发布时间:2016/10/11 21:55:00
--  回复:(有点蓝)注意json的层次dr("title") = data.i...
dr("title") = data.item(0).content.news_item(0).title


MessageBox.Show(data.total_count)      得到56
MessageBox.Show(data.item_count)       得到2
MessageBox.Show(data.item(0).media_id)  提示找不到成员

返回的值我需要得到title 、 author 、 url  、update_time这四个值

下面是JSON我清理下后的内容

{"item":[{

"media_id":"gwmL5Qi-95lfkB7dnSYm66KXipsv1QpQgoy7exD5HFY",

"content":{

"news_item":[{

"title":"【震惊】新买的手机号发现绑定支付宝!竟能从对方账户转钱,速",

"author":"",

"digest":"",

"content":"

"content_source_url":"",

"thumb_media_id":"gwmL5Qi-95lfkB7dnSYm6w02eWFqhdIVKaR4q9V_CU",

"show_cover_pic":0,

"url":"http:\\/\\/mp.weixin.qq.com\\/s?__biz=MzAxMzAzNjg0Mw==&mid=503499675&idx=1&sn=ce1c640d5c575b834119199f23fd6f54&chksm=005ed7df37295ec96eb7cc15cc7725cc90214bca813e0ac6f11a67b2151b70d69517be19d6fc#rd",

"thumb_url":"http:\\/\\/mmbiz.qpic.cn\\/mmbiz_png\\/zMPVPsVaWBBjSticlTSg3fIyXgExbRrHsrzB5uP8rRVX7pqHk9XpBgWZUnNfVk57FWqZDxVzicql1C7Ud63GUE3w\\/0?wx_fmt=png"},

 

{"title":"国庆节大事纪:19城市调控楼市、金价大暴跌",

"author":"",

"digest":"",

"content":"

"content_source_url":"",

"thumb_media_id":"gwmL5Qi-95lfkB7dnSYm63LULSyh2-IKy408OrVs56s",

"show_cover_pic":0,

"url":"http:\\/\\/mp.weixin.qq.com\\/s?__biz=MzAxMzAzNjg0Mw==&mid=503499675&idx=2&sn=8c9b36dc892f017023a5eca7ba2cacfd&chksm=005ed7df37295ec99cddd95b02b2f75bb305dce1b8a4fcc770e6ce5c018832bf2c1566a9c8c0#rd",

"thumb_url":"http:\\/\\/mmbiz.qpic.cn\\/mmbiz_png\\/zMPVPsVaWBBjSticlTSg3fIyXgExbRrHsqBe6BoxsQBU8mZHgmy503fPq7HghUG1xGnicHFWiaTC0RhehPVDSyxCw\\/0?wx_fmt=png"},

], "create_time":1475895222,

"update_time":1475895242},

"update_time":1475895242}],

"total_count":56,

"item_count":2}

[此贴子已经被作者于2016/10/11 21:56:51编辑过]