Android与Javascript交互JSON对象

webview.loadUrl(“JavaScript:”+callbackFunction+”(‘”+data+”’)”); 这种方式将jsonobject类型的data传给js,因为js那边得到就是一个string的对象。


与此同时,js主动调用android的对象方式,android也无法返回给js一个jsonobject,需要js做一下转换,例如:


Android 代码:

  1. WebView mWebView = (WebView) this.findViewById(R.id.webview);  
  2. WebSettings settings = mWebView.getSettings();  
  3. settings.setJavaScriptEnabled(true);  
  4. settings.setPluginsEnabled(true);  
  5. settings.setAllowFileAccess(true);  
  6.   
  7. settings.setCacheMode(WebSettings.LOAD_NO_CACHE);  
  8. mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//不加上,会显示白边  
  9. String  url=”file:///android_asset/t.html”; //js代码卸载t.html里  
  10. NavigationInstance navigation =new NavigationInstance(this);  
  11. mWebView.addJavascriptInterface(navigation, ”Navigation”);  
WebView mWebView = (WebView) this.findViewById(R.id.webview);
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setPluginsEnabled(true);
settings.setAllowFileAccess(true);

settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//不加上,会显示白边
String  url="file:///android_asset/t.html"; //js代码卸载t.html里
NavigationInstance navigation =new NavigationInstance(this);
mWebView.addJavascriptInterface(navigation, "Navigation");



NavigationInstance里的代码:

  1. @Override  
  2. public JSONObject GetManeuverInfo() {  
  3. try{  
  4. JSONObject test=new JSONObject();  
  5. test.put(”maomao”“value”);  
  6. return test;  
  7. //return new JSONObject(bean.ManeuverInfo);  
  8. }catch(Exception e){  
  9. Log.e(TAG, ”“,e);  
  10. }  
  11. return null;  
  12. }  
@Override
public JSONObject GetManeuverInfo() {
try{
JSONObject test=new JSONObject();
test.put("maomao", "value");
return test;
//return new JSONObject(bean.ManeuverInfo);
}catch(Exception e){
Log.e(TAG, "",e);
}
return null;
}




t.html里的代码:

[javascript] view plain copy
print ?
  1. function testAPI(el){  
  2.     console.log(”———testAPI———”);  
  3.     eval(”var obj = ”+Navigation.GetManeuverInfo());   
  4.       
  5.     alert(’typeof:’+typeof(obj));  
  6.     alert(’maomao:’+obj.maomao);  
  7.     alert(’obj:’+obj);  
  8.   }  
        function testAPI(el){
            console.log("---------testAPI---------");
            eval("var obj = "+Navigation.GetManeuverInfo()); 

            alert('typeof:'+typeof(obj));
            alert('maomao:'+obj.maomao);
            alert('obj:'+obj);
          }

如果直接写成  Navigation.GetManeuverInfo.maomao是会提示undefined,因为js那边只得到了一个string对象而已,它不知道maomao是个key。

通过eval将其转化成表达式就可以调用obj.maomao得到value。


在此ps一下iOS,貌似人家支持webview很好,js可以直接获取到json对象.



默认t.html加载会自动执行testAPI函数,结果如下:


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要遍历一个JSON对象,可以使用JavaScript中的for-in循环。首先,将获取的数据转换为JSON对象,可以使用JSON.parse(data)方法或者jQuery库中的jQuery.parseJSON(json)方法。接下来,使用for-in循环遍历JSON对象,通过得到的key和value来进行相应的操作。如果value是一个对象,可以使用typeof value == "object"进行判断,并使用递归的方式遍历内部的对象。以下是一个示例方法来获取JSON对象中的值: ``` function getJsonValue(obj, name){ var result = null; var value = null; for(var key in obj){ value = obj[key]; if(key == name){ return value; } else { if(typeof value == "object"){ result = getJsonValue(value, name); } } } return result; } var jsonobj = { "semantic":{ "taskId":"8.4.3" }, "history":"cn.xxxx.fund" }; var taskId = getJsonValue(jsonobj, "history"); console.log(taskId); ``` 这个方法将通过递归的方式遍历JSON对象,根据给定的name参数,返回对应的值。在这个示例中,我们使用了jsonobj***fund"作为结果输出到控制台。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [jsjson对象的遍历和其他方法处理](https://blog.csdn.net/u012409048/article/details/115920371)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [js 遍历 JSON数据](https://blog.csdn.net/fox_bert/article/details/100594710)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值