从httpconnection解析Json对象 [英] Parsing Json Object from httpconnection

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

问题描述

亲爱的所有人;
我编写了一个php查询,该查询返回以下JSON字符串:
[{"cnt":"0","user":"tamer"},{"cnt":"1","user":"Jose"}]

所以在我的黑莓手机应用程序中
我写了以下内容:

Dear All;
I wrote a php query which return the following JSON string :
[{"cnt":"0","user":"tamer"},{"cnt":"1","user":"Jose"}]

so In my blackberry application
I wrote the following :

<pre lang="java">  returnString = returnStringBuffer.toString();
	             Dialog.alert(returnString);
	                // examine return string
	             
	             String jsonString =new String("{");
	             jsonString= jsonString + "\""; 
	             jsonString= jsonString +"myArray\" : ";   
	             jsonString= jsonString +returnString;
	             jsonString= jsonString +"}";
	             Dialog.alert(jsonString);
	          // Now the call should succeed
	    
	                JSONObject myjson = new JSONObject(jsonString);
                 
	                JSONArray nameArray = myjson.names();
	               JSONArray valArray = myjson.toJSONArray(nameArray);
	             
	              Dialog.alert( nameArray.getString(0));
	              Dialog.alert( valArray.getString(0));
	             
	              Dialog.alert( nameArray.getString(0));
	                if (returnString.indexOf("1") != -1)
	                {
	                    ret = true;
	                }




所以returnString是httpconection中的字符串,现在可以正常使用了.

现在,我想将returnString分配给JsonObject ...然后我要访问例如这样的内容:

valArray ["cnt"] [0];
valArray ["user"] [0];

应该怎么做???
问候...




So the returnString is the string from the httpconection it works fine uptill now.

Now I want to assign the returnString to JsonObject ... then I want to access for example like that :

valArray["cnt"][0];
valArray["user"][0];

how it should be done ???
regards...

推荐答案

通过HTTP获得的returnstring不能作为JSON对象,而可以作为JSON数组.除此之外,您的代码还会做一些奇怪的事情.新近实例化的JSONObject通过等于返回字符串的比较.您想在这里做什么?也许您应该先对Java进行一些研究.

问候,

—MRB
The returnstring you got via HTTP does not qualify as a JSON object, but rather as a JSON array. Besides that your code does strange things. A JSONObject freshly instantiated is compared via equals to the return string. What are you trying to do here? Maybe you should do some studies on Java first.

Regards,

—MRB


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

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