当前位置: 首页 > 知识库问答 >
问题:

无法将JSONObject转换为com。作语法分析分析地质点

西门安宁
2023-03-14

我正在使用以下命令在JsonObject中保存geopoint:

        JSONObject obj=new JSONObject();
        JSONArray jA=new JSONArray();

        if(lx.size()==0){
            Toast.makeText(ctx, "No location to upload now", Toast.LENGTH_LONG).show();
        }
        else{
            for(int uq=0;uq<lx.size();uq++){
                Double latit=lx.get(uq).getLatit();
                Double longit=lx.get(uq).getLongit();
                ParseGeoPoint pgPoint=new ParseGeoPoint(latit,longit);
                jA.put(pgPoint);


            }
            try {
                obj.put("locations",jA);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

在此之后,我将发送这个Jsonobject来解析云。

                po.put("historyfile", obj);
                po.saveInBackground(new SaveCallback() {

现在我试着用这个把它拿回来:

            JSONArray locations;
            ParseGeoPoint
            locations = obj.getJSONArray("locations");
            for (int yx = 0; yx < locations.length(); yx++) {
                pg =(ParseGeoPoint)locations.get(yx);

               //draw this geopoint on googlemap
            }

首先在线路上:

             pg =(ParseGeoPoint)locations.get(yx);

我得到了类型不匹配的错误。当我将其输入到parse对象中时,它运行良好;但是,当我尝试运行apk时,出现了以下错误:

            Caused by: java.lang.ClassCastException: org.json.JSONObject cannot be cast to com.parse.ParseGeoPoint

共有1个答案

裴承安
2023-03-14

尝试转到parse.com数据页面并将JSONObject复制粘贴到jsonlint.com,然后您将看到pg在JSONObject中保存后的确切外观。它本质上是一个字符串值,可以被视为JSONObject,但String或JSONObject无法直接转换为ParseGeoPoint。充其量您可以深入检索lat/lon值:

JSONArray locations;
ParseGeoPoint
locations = obj.getJSONArray("locations");
for (int yx = 0; yx < locations.length(); yx++) {
    pg = locations.getJSONObject(yx);
    Double lat = pg.getDouble("latitude")
    Double lon = pg.getDouble("longitude")
    //draw this geopoint on googlemap
}

现在,我还没有像您那样尝试将ParseGeoPoint保存为JSONObject,因此不确定上述内容是否正确,只是一个示例。

如果您只需要能够存储和检索位置,那么您可以简单地执行以下操作:

...
for(int uq=0;uq<lx.size();uq++){
    Double latit=lx.get(uq).getLatit();
    Double longit=lx.get(uq).getLongit();
    JSONObject jsonPos = new JsonObject()
    json.put("lat", latit);
    json.put("lon", longit);
    jA.put(jsonPos);
}
...

而且

JSONArray locations;
ParseGeoPoint
locations = obj.getJSONArray("locations");
for (int yx = 0; yx < locations.length(); yx++) {
    pg =locations.getJSONObject(yx);
    Double lat = pg.getDouble("lat")
    Double lon = pg.getDouble("lon")
    //draw this geopoint on googlemap
}
 类似资料:
  • } 另一个UserFunction类,其中服务调用:公共类UserFunction{

  • 我在使用parse检索javascript/html中的createdAt字段时遇到麻烦。这是我如何以超文本标记语言显示当前字段。作为参考,我从这里获得的原始教程:http://code.tutsplus.com/tutorials/getting-started-with-parse--net-28000 这是我如何将其保存在javascript中。当网页显示时,内容是正确的,但createdA

  • 问题内容: 我是新手,但正在收到以下异常: 在try部分的第一行本身。 请帮助我删除它。这是我的代码: 这是我从我的php文件中获取的json 问题答案: 这个 应该 表示json数组节点 表示json对象节点

  • 顾名思义,一个命令式语言是由一个个“命令”为单元组成,不过一般很少用命令这个词,而是细分一下,比较低级的语言用指令(instruction),高低的语言一般认为是一个语句(statement,以后简称stmt),词法分析只将一段高级语言代码分解成一个个词,接下来还要做语句层面的分析,最终目标是生成抽象语法树(ast) 代码: a = 1; s = 0; while (a <= 100)