【问题标题】:Json decoding in AndroidAndroid中的Json解码
【发布时间】:2023-03-04 00:50:02
【问题描述】:

我在解码 json 字符串时遇到了困难。下面是我的 json 字符串:

{
    "udeals":"[{\"vName\":\"bussinessname3\",\"vAddress\":\"address3\",\"vState\":\"Kerala\",\"vZip\":\"232\",\"vCountry\":\"Algeria\",\"id\":\"3\",\"dealCategoryID\":\"2\",\"dealCaption\":\"deal3\",\"dealurl\":\"http:\\/\\/192.168.0.8\\/ppppp\\/deals\\/deal3-3\\/\",\"dealImage\":\"http:\\/\\/192.168.0.8\\/ppppp\\/images\\/deals\\/thumb390x256\\/Duizl1.jpg\",\"dealPrice\":\"50.00\",\"dealCost\":\"100.00\",\"dealSave\":-50,\"dealOfferRate\":\"50.00\",\"jsDeadLine\":{\"seconds\":0,\"minutes\":0,\"hours\":0,\"mday\":5,\"wday\":3,\"mon\":1,\"year\":2011,\"yday\":4,\"weekday\":\"Wednesday\",\"month\":\"January\",\"0\":1294165800},\"unlockedStamp\":{\"seconds\":53,\"minutes\":56,\"hours\":18,\"mday\":5,\"wday\":3,\"mon\":1,\"year\":2011,\"yday\":4,\"weekday\":\"Wednesday\",\"month\":\"January\",\"0\":1294234013},\"description\":\"Google URL Shortener at goo.gl is used by Google products to create short URLs that can be easily shared, tweeted, or emailed to friends.\"},{\"vName\":\"bussinessname20010\",\"vAddress\":\"address20010\",\"vState\":\"Kerala\",\"vZip\":\"232\",\"vCountry\":\"Argentina\",\"id\":\"20010\",\"dealCategoryID\":\"2\",\"dealCaption\":\"deal20010\",\"dealurl\":\"http:\\/\\/192.168.0.8\\/ppppp\\/deals\\/deal20010-20010\\/\",\"dealImage\":\"http:\\/\\/192.168.0.8\\/ppppp\\/images\\/deals\\/thumb390x256\\/oCcfTj.jpg\",\"dealPrice\":\"50.00\",\"dealCost\":\"100.00\",\"dealSave\":-50,\"dealOfferRate\":\"50.00\",\"jsDeadLine\":{\"seconds\":0,\"minutes\":0,\"hours\":0,\"mday\":28,\"wday\":2,\"mon\":12,\"year\":2010,\"yday\":361,\"weekday\":\"Tuesday\",\"month\":\"December\",\"0\":1293474600},\"unlockedStamp\":{\"seconds\":53,\"minutes\":56,\"hours\":18,\"mday\":5,\"wday\":3,\"mon\":1,\"year\":2011,\"yday\":4,\"weekday\":\"Wednesday\",\"month\":\"January\",\"0\":1294234013},\"description\":\"this is\"}]"
}

谁能给我一个如何在Android中解析这个的解决方案...

【问题讨论】:

    标签: android json


    【解决方案1】:

    这个json可以用下面的代码解析。

    str="<The Json>"
    try {
                        JSONObject jObject=new JSONObject(str);
                        JSONArray menuObject = new JSONArray(jObject.getString("udeals"));
    
                        for (int i = 0; i<menuObject.length(); i++) {
                            System.out.println("length="+menuObject.length());
                            System.out.println("vName="+menuObject.getJSONObject(i)
                                    .getString("vName"));
                            System.out.println("vAddress="+menuObject.getJSONObject(i).getString(
                                    "vAddress"));
                            System.out.println("vState="+menuObject.getJSONObject(i)
                                    .getString("vState"));
                            System.out.println("vZip="+menuObject.getJSONObject(i).getString(
                                    "vZip"));
                            System.out.println("vCountry="+menuObject.getJSONObject(i)
                                    .getString("vCountry"));
                            System.out.println("dealCategoryID="+menuObject.getJSONObject(i)
                                    .getString("dealCategoryID"));
                            System.out.println("dealCaption="+menuObject.getJSONObject(i).getString(
                                    "dealCaption"));
                            System.out.println("dealImage="+menuObject.getJSONObject(i)
                                    .getString("dealImage"));
                            System.out.println("dealPrice="+menuObject.getJSONObject(i).getString(
                                    "dealPrice"));
                            System.out.println("dealCost="+menuObject.getJSONObject(i)
                                    .getString("dealCost"));
                            System.out.println("dealSave="+menuObject.getJSONObject(i).getString(
                                    "dealSave"));
                            System.out.println("dealOfferRate="+menuObject.getJSONObject(i)
                                    .getString("dealOfferRate"));
                            System.out.println("jsDeadLine="+menuObject.getJSONObject(i).getString(
                                    "jsDeadLine"));
                            jsDeadLine=menuObject.getJSONObject(i).getString("jsDeadLine");
                            JSONObject deadLine= new JSONObject(jsDeadLine);
                            String seconds = deadLine.getString("seconds");
                            String minutes = deadLine.getString("minutes");
                            String hours = deadLine.getString("hours");
                            String mday = deadLine.getString("mday");
                            String wday = deadLine.getString("wday");
                            String mon = deadLine.getString("mon");
                            String year = deadLine.getString("year");
                            String yday = deadLine.getString("yday");
                            String weekday = deadLine.getString("weekday");
                            String month = deadLine.getString("month");
                            System.out.println("seconds:"+seconds);
                            System.out.println("minutes:"+minutes);
                            System.out.println("hours:"+hours);
    
    
    
                            System.out.println("unlockedStamp="+menuObject.getJSONObject(i)
                                    .getString("unlockedStamp"));
    
                            unlockedStamp=menuObject.getJSONObject(i).getString("unlockedStamp");
                            JSONObject unlocked= new JSONObject(unlockedStamp);
                            String unlockedseconds = unlocked.getString("seconds");
                            String unlockedminutes = unlocked.getString("minutes");
                            String unlockedhours = unlocked.getString("hours");
                            String unlockedmday = unlocked.getString("mday");
                            String unlockedwday = unlocked.getString("wday");
                            String unlockedmon = unlocked.getString("mon");
                            String unlockedyear = unlocked.getString("year");
                            String unlockedyday = unlocked.getString("yday");
                            String unlockedweekday = unlocked.getString("weekday");
                            String unlockedmonth = unlocked.getString("month");
                            System.out.println("unlockedseconds:"+unlockedseconds);
                            System.out.println("unlockedminutes:"+unlockedminutes);
                            System.out.println("unlockedhours:"+unlockedhours);
    
    
                            System.out.println("description="+menuObject.getJSONObject(i).getString(
                                    "description"));
                            System.out.println("id="+menuObject.getJSONObject(i)
                                    .getString("id"));
                            System.out.println("dealurl="+menuObject.getJSONObject(i).getString(
                                    "dealurl"));
                        }
    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
    
    
                }
    
    
    
    
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
    

    【讨论】:

    • 非常感谢。你是卡利特人吗?
    【解决方案2】:
    JSONObject json = new JSONObject(yourstring);
    JSONArray jsa = json.getJSONArray("udeals");
    ....
    

    真的很简单:http://developer.android.com/reference/org/json/JSONObject.html

    【讨论】:

    • 我注意到 udeals 实际上不是一个数组,而是一个字符串(用双引号括起来)。这还能用吗?这可能是罗勒遇到问题的地方......要解决它,他不得不说: JSONObject obj = new JSONObject(yourstring); JSONArray arr = new JSONArray(json.getString("udeals"));对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多