[英]parse json java result null
I am stucking with parse json in java. 我坚持用java解析json。 Here is my code:
这是我的代码:
package url.process;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class jsonArray implements JSONAware{
private long l;
public jsonArray(long l){
this.l=l;
}
public long getArray(){
return l;
}
public void setArray(long l){this.l=l;}
@Override
public String toJSONString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append("\""+getArray()+"\"");
sb.append("}");
return sb.toString();
}
public static void main(String[] args){
// doc doi tuong thanh chuoi
List <jsonArray> listjsonarray = new ArrayList<jsonArray>(){
{
add( new jsonArray(76543456));
add( new jsonArray(112233445));
add( new jsonArray(546372));
add( new jsonArray(9876553));
}
};
System.out.println(JSONArray.toJSONString(listjsonarray));
//doc chuoi thanh doi tuong
String jsonString = "[{\"76543456\"},"+"{\"112233445\"},"+"{\"546372\"},"+"{\"9876553\"}]";
try{
JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray) jsonParser.parse(jsonString);
for(int i =0;i<jsonArray.size();i++){
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
long l = Long.parseLong((String) jsonObject.get("l"));
jsonArray ja = new jsonArray(l);
System.out.println("Elements is "+ja.getArray());
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
} }
The result is : [{"76543456"},{"112233445"},{"546372"},{"9876553"}] null 结果为:[{“ 76543456”},{“ 112233445”},{“ 546372”},{“ 9876553”}] null
I do not know to parse this array above. 我不知道解析上述数组。 Please help me, thank you so much and have a good time.
请帮助我,非常感谢,并度过愉快的时光。
Your JSON is invalid. 您的JSON无效。 Braces
{}
indicate a JSON object which contains key-value pairs. 大括号
{}
表示包含键值对的JSON对象。 But you are putting JSON strings in them. 但是您要在其中放入JSON字符串。
[{"76543456"},{"112233445"},{"546372"},{"9876553"}]
Perhaps you meant to have 也许你本来打算
["76543456","112233445","546372","9876553"]
The JSON format is described here . 此处描述JSON格式。
You'll have other problems later when trying to cast a String
to a JSONObject
. 稍后,当您尝试将
String
JSONObject
转换为JSONObject
时,还会遇到其他问题。 Handle those appropriately. 妥善处理。 If you're storing JSON strings, you should get back Java
String
objects. 如果要存储JSON字符串,则应取回Java
String
对象。
JSON is a key value data structure. JSON是键值数据结构。 For example, a JSON object is as follow:
例如,JSON对象如下:
{"name" : "John Smith"}
The string you input there doesn't follow the convention of JSON, therefore the program couldn't work 您在此处输入的字符串未遵循JSON的约定,因此该程序无法正常工作
The null
output is because of null
输出是由于
}catch(Exception e){
System.out.println(e.getMessage());
});
The json is not valid and therefore you get a ParseException
and this exception has no message. json无效,因此您收到
ParseException
并且此异常没有消息。
As I can see you want to get the l
property of the JSONObject
如我所见,您想获取
JSONObject
的l
属性
long l = Long.parseLong((String) jsonObject.get("l"));
in this case the correct json would be 在这种情况下,正确的json将是
String jsonString = "[{\"l\": \"76543456\"},"+"{\"l\": \"112233445\"},"+"{\"l\": \"546372\"},"+"{\"l\": \"9876553\"}]";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.