如何在 LWUIT 中解析 JSON 字符串

发布于 2024-12-03 17:31:37 字数 127 浏览 2 评论 0原文

如何在 LWUIT 中解析 JSON 对象,请给我一些示例或一些我可以阅读此内容的链接。假设我有下面给出的对象。 "{'公会': '猩红之地', '地区': '我们', '领域': '凯勒斯特拉兹', '时间戳': 1311860040}"

How to parse a JSON object in LWUIT,give me some example or some link from where i can read this.Suppose i have the objects given below.
"{'guild': 'Crimson', 'region': 'us', 'realm': 'Caelestrasz', 'timestamp': 1311860040}"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

朕就是辣么酷 2024-12-10 17:31:37

Json 示例代码:此代码适用于 json。

 package com.ndtv.parser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import com.ndtv.callback.jsonActivelistener;
import com.ndtv.datatype.StockActiveItem;

 import json.me.JSONArray;
 import json.me.JSONException;
 import json.me.JSONObject;

 public class StockActiveParser {
public Vector jsonObjVector = new Vector();
public JSONArray arrayObj = null;
public String name,LastPrice;
protected jsonActivelistener mjsonListener;

public static boolean ParserCanceled = false;
public void setjsonListener(jsonActivelistener listener) {
    mjsonListener = listener;
}

// Non-blocking.
public void parser(final String url) {
    Thread t = new Thread() {
        public void run() {
            // set up the network connection
            try {
                jsonParse(url);
            }
            catch (Exception e) {
                mjsonListener.parserExceptionListing(e);
            }
            mjsonListener.parseDidFinishListing();
        }
    };
    t.start();
}

protected void jsonParse(String url) {
    StringBuffer stringBuffer = new StringBuffer();
    InputStream is = null;
    HttpConnection hc = null;
    System.out.println(url);
    try {
        hc = (HttpConnection)Connector.open(url);
        is = hc.openInputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            stringBuffer.append((char) ch);
        }
    }

    catch (SecurityException se) {
        System.out.println("security exception:"+se);
    }
    catch (NullPointerException npe) {
        System.out.println("null pointer excception:"+npe);
    }
    catch (IOException ioe) {
        System.out.println("io exception:"+ioe);
    }
    try{
        hc.close();
        is.close();
    }catch(Exception e) {
        System.out.println("Error in MostActivePareser Connection close:"+e);
        e.printStackTrace();
    }

    String jsonData = stringBuffer.toString();
    try {
        JSONObject js = new JSONObject(jsonData);
        JSONArray js2 = js.getJSONArray("values");
        System.out.println(js2.length());


        for (int i = 0; i < js2.length(); i++) {
            StockActiveItem item = new StockActiveItem();

            JSONObject jsObj = js2.getJSONObject(i);
            item.name = jsObj.getString("name");
            item.last_traded_price = jsObj.getString("last_traded_price");
            item.change = jsObj.getString("change");
            item.price_diff = jsObj.getString("price_diff");
            item.chart=jsObj.getString("chart");
            item.company_id=jsObj.getString("company_id");
            mjsonListener.itemParsedListing(item);
        }
    } catch (JSONException e1) {
        System.out.println("Json Data error:"+e1);
        e1.printStackTrace();
    }
    catch (NullPointerException e) {
        System.out.println("null error:"+e);
    }
}
 }


public class StockActiveItem 
{
public String name ="";
public String last_traded_price ="";
public String change="";
public String price_diff ="";
public String chart="";
public String company_id="";
public String year_high="";
public String year_low="";

}

您只需替换名称,例如公会替换名称。如果有任何疑问,请询问我。

Json Example Code:This Code will work for json.

 package com.ndtv.parser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import com.ndtv.callback.jsonActivelistener;
import com.ndtv.datatype.StockActiveItem;

 import json.me.JSONArray;
 import json.me.JSONException;
 import json.me.JSONObject;

 public class StockActiveParser {
public Vector jsonObjVector = new Vector();
public JSONArray arrayObj = null;
public String name,LastPrice;
protected jsonActivelistener mjsonListener;

public static boolean ParserCanceled = false;
public void setjsonListener(jsonActivelistener listener) {
    mjsonListener = listener;
}

// Non-blocking.
public void parser(final String url) {
    Thread t = new Thread() {
        public void run() {
            // set up the network connection
            try {
                jsonParse(url);
            }
            catch (Exception e) {
                mjsonListener.parserExceptionListing(e);
            }
            mjsonListener.parseDidFinishListing();
        }
    };
    t.start();
}

protected void jsonParse(String url) {
    StringBuffer stringBuffer = new StringBuffer();
    InputStream is = null;
    HttpConnection hc = null;
    System.out.println(url);
    try {
        hc = (HttpConnection)Connector.open(url);
        is = hc.openInputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            stringBuffer.append((char) ch);
        }
    }

    catch (SecurityException se) {
        System.out.println("security exception:"+se);
    }
    catch (NullPointerException npe) {
        System.out.println("null pointer excception:"+npe);
    }
    catch (IOException ioe) {
        System.out.println("io exception:"+ioe);
    }
    try{
        hc.close();
        is.close();
    }catch(Exception e) {
        System.out.println("Error in MostActivePareser Connection close:"+e);
        e.printStackTrace();
    }

    String jsonData = stringBuffer.toString();
    try {
        JSONObject js = new JSONObject(jsonData);
        JSONArray js2 = js.getJSONArray("values");
        System.out.println(js2.length());


        for (int i = 0; i < js2.length(); i++) {
            StockActiveItem item = new StockActiveItem();

            JSONObject jsObj = js2.getJSONObject(i);
            item.name = jsObj.getString("name");
            item.last_traded_price = jsObj.getString("last_traded_price");
            item.change = jsObj.getString("change");
            item.price_diff = jsObj.getString("price_diff");
            item.chart=jsObj.getString("chart");
            item.company_id=jsObj.getString("company_id");
            mjsonListener.itemParsedListing(item);
        }
    } catch (JSONException e1) {
        System.out.println("Json Data error:"+e1);
        e1.printStackTrace();
    }
    catch (NullPointerException e) {
        System.out.println("null error:"+e);
    }
}
 }


public class StockActiveItem 
{
public String name ="";
public String last_traded_price ="";
public String change="";
public String price_diff ="";
public String chart="";
public String company_id="";
public String year_high="";
public String year_low="";

}

you just replace name, for example guild replacing name.If any doubt ask me.

尛丟丟 2024-12-10 17:31:37

使用 LWUIT JSONParser 并解析 JSON 格式字符串。只需使用最新 LWUIT 1.5 中的 MIDP_IO.jar 即可。

Use LWUIT JSONParser and parser the JSON format string. Just use MIDP_IO.jar from latest LWUIT 1.5 for this.

温柔嚣张 2024-12-10 17:31:37

我能够在我的应用程序中成功使用下面给定链接中提供的示例代码。

http://jimmod.com/blog/2010/ 03/java-me-j2me-json-implementation-tutorialsample/
http://jimmod.com/ blog/2011/09/java-me-j2me-json-implementation-for-array-object/

顺便说一句,最新的 JSON ME 库可以在这里找到:https://github.com/upictec/org.json.me/

夏日落 2024-12-10 17:31:37

您可以使用 JSON jar 并将其导入到您的项目中。之后创建一个 JSON 对象,您可以相应地使用该对象的 optString 或 getString 方法来获取值。

You can use JSON jar and and import that in your project. After that create a JSON object and you can use optString or getString methods of that object accordingly to get the values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文