[英]creating json string using JSONObject and JSONArray
I have data like this: 我有这样的数据:
NewsItem : 新闻项目:
There may be many NewsItems say 10. I have to send them to jquery. 可能有许多NewsItems说10。我必须将它们发送到jquery。
I am doing this: 我正在这样做:
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();
for(int i = 0 ; i< list.size() ; i++){
p = list.get(i);
arr.put(p.getId());
arr.put(p.getTitle());
arr.put(new MyDateFormatter().getStringFromDateDifference(p.getCreationDate()));
arr.put(getTrimmedText(p.getText()));
obj.put(""+i,arr);
arr = new JSONArray();
}
This will create a JSON string like this : {"1":["id","title","date","txt"],"2":[......and so on...
这将创建一个JSON字符串,如下所示: {"1":["id","title","date","txt"],"2":[......and so on...
Is that correct way of doing this? 这是正确的方法吗?
How can I parse this string so that I can get each news item object in jQuery so that I can access attr. 如何解析此字符串,以便可以在jQuery中获取每个新闻项对象,以便可以访问attr。
Like this: 像这样:
obj.id,
obj.title
Or if this is wrong way of creating JSON string, please suggest some better way with example of parsing in jQuery. 或者,如果这是创建JSON字符串的错误方法,请提出一些更好的方法,并以jQuery分析为例。
I believe that you're organizing your data backwards. 我相信您正在反向整理数据。 It seems that you want to use an array of NewsItems
, and if so, then your java JSON generation code should look like this: 似乎您想使用NewsItems
数组,如果是这样,那么您的Java JSON生成代码应如下所示:
JSONObject obj = new JSONObject();
JSONArray arr = new JSONArray();
for(int i = 0 ; i< list.size() ; i++)
{
p = list.get(i);
obj.put("id", p.getId());
obj.put("title", p.getTitle());
obj.put("date". new MyDateFormatter().getStringFromDateDifference(p.getCreationDate()));
obj.put("txt", getTrimmedText(p.getText()));
arr.put(obj);
obj = new JSONObject();
}
Now your JSON string will look something like this: 现在,您的JSON字符串将如下所示:
[{"id": "someId", "title": "someTitle", "date": "dateString", "txt": "someTxt"},
{"id": "someOtherId", "title": "someOtherTitle", "date": "anotherDateString", "txt": "someOtherTxt"},
...]
Assuming that your NewsItem gettors return Strings
. 假设您的NewsItem gettor返回Strings
。 The JSONObject method put
is overloaded to take primitive types also, so if, eg your getId
returns an int
, then it will be added as a bare JSON int
. put
的JSONObject方法也被重载以采用原始类型,因此,例如,如果您的getId
返回一个int
,则它将作为裸JSON int
添加。 I'll assume that JSONObject.put(String, Object)
calls toString
on the value, but I can't verify this. 我假设JSONObject.put(String, Object)
在值上调用toString
,但是我无法验证这一点。
Now in javascript, you can use such a string directly: 现在,在javascript中,您可以直接使用这样的字符串:
var arr =
[{"id": "someId", "title": "someTitle", "date": "dateString", "txt": "someTxt"},
{"id": "someOtherId", "title": "someOtherTitle", "date": "anotherDateString", "txt": "someOtherTxt"}];
for (i = 0; i < arr.length; i++)
alert(arr[i].title); // should show you an alert box with each first title
The idea of the json object is the same as a dictionary/map where you have keys and values assigned to those keys, so what you want to construct would be something like this: json对象的概念与字典/映射相同,在字典/映射中您具有键和分配给这些键的值,因此要构造的内容如下所示:
{"1": {"title": "my title", "date": "17-12-2011", "text": "HELLO!"}, "2": ....}
where the "1" is the id and the contents is another dictionary/map with the info. 其中“ 1”是ID,内容是带有信息的另一个词典/地图。
lets say you assigned the object to a variable named my_map , now you will be able to handle it as: 假设您将对象分配给了一个名为my_map的变量,现在您可以将其处理为:
my_map.1.title
my_map.3.text
...
to iterate over it just use: 要迭代它,只需使用:
for (info in my_map){
data = my_map[info];
//do what you need
}
For converting JSON object to JSON string use 要将JSON对象转换为JSON字符串,请使用
JSON.stringify(json_object)
For reverse use: 对于反向使用:
JSON.parse(json_string)
This is the correct way - 这是正确的方法-
final JSONArray arr = new JSONArray();
for(int i = 0 ; i< list.size() ; i++) {
final JSONObject obj = new JSONObject();
p = list.get(i);
obj.add("id", p.getId());
obj.add("title", p.getTitle());
obj.add("date", new MyDateFormatter().getStringFromDateDifference(p.getCreationDate()));
obj.add("txt", getTrimmedText(p.getText()));
arr.add(obj);
}
This will generate 这将产生
[{"id": 1, "date": 222, "title": "abc", "txt": "some text"}, {...}]
Now, when you parse the json at client end, you can iterate over the array and for each json object you can access as - 现在,当您在客户端解析json时,可以遍历数组,对于每个json对象,您都可以按以下方式访问-
obj.id
or obj["id"]
obj.id
或obj["id"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.