黑莓JSON解析器 [英] Blackberry json parser

查看:584
本文介绍了黑莓JSON解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个黑莓测试一个简单的JSON解析器。我发现这个例如,贯彻我的$ C $ ç如下:

I want to create a simple json parser for BlackBerry to test. I found this example, and implement my code as follows

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import org.json.me.JSONArray;
import org.json.me.JSONObject;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;


public final class MyScreen extends MainScreen
{
HttpConnection conn = null;
InputStream in = null;
String _response = null;
Vector _adBeanVector=null;
int code;
public MyScreen()
{             
    setTitle("MyTitle");
    try 
    {
    StringBuffer url=new StringBuffer().append("http://codeincloud.tk/json_android_example.php");
    conn = (HttpConnection) Connector.open(url.toString(), Connector.READ);
    conn.setRequestMethod(HttpConnection.GET);

    code = conn.getResponseCode();
    if (code == HttpConnection.HTTP_OK) {
    in = conn.openInputStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[in.available()];
    int len = 0;
    while (-1 != (len = in.read(buffer))) {
    out.write(buffer); 
    }
    out.flush();
    _response = new String(out.toByteArray());
    JSONObject resObject=new JSONObject(_response);
    String _key =resObject.getString("Insert Json Key");

    _adBeanVector = new Vector();
    JSONArray newsArray = resObject.getJSONArray("Insert Json Array Key");
    if(newsArray.length() > 0)
    {
    for (int i=0 ;i <  newsArray.length() ; i++) 
    {
    Vector _adElementsVector=new Vector();
    JSONObject newsObj = newsArray.getJSONObject(i);
    _adElementsVector.addElement(newsObj.getString("message"));
    //_adElementsVector.addElement(newsObj.getString("Insert Json Array Element Key2"));
    _adBeanVector.addElement(_adElementsVector);
    }
    }
    if (out != null){
    out.close();
    }
    if (in != null){
    in.close();
    }
    if (conn != null){
    conn.close();
    }
    }

    } catch (Exception e) 
    {
    Dialog.alert(e.getMessage());
    }
}
}

但我不能修改我的目的。任何一个可以帮助我修改此code样品访问这个 JSON。我非常新的这个主题领域。

but I cannot modify it for my purpose. Can any one help me to modify this code sample to access this json. I'm very much new to this subject area.

感谢您提前。

推荐答案

试试这个 -

 JSONObject resObject=new JSONObject(_response);
 String _name=resObject.getString("name"); //This will get the result as Froyo
 String _version=resObject.getString("version");  //This will get the result as Android 2.2   
 Dialo.alert("Name : "+_name+" and Version : "+_version);

这篇关于黑莓JSON解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆