使用JSONObject和JSONArray创建json字符串 [英] creating json string using JSONObject and JSONArray

查看:131
本文介绍了使用JSONObject和JSONArray创建json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据:

NewsItem:


  • id

  • title

  • date

  • txt

  • id
  • title
  • date
  • txt

可能有很多NewsItems说10.我必须将它们发送到jquery。

There may be many NewsItems say 10. I have to send them to jquery.

我这样做:

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();
}

这将创建一个这样的JSON字符串: { 1:[id,title,date,txt],2:[......依此类推......

This will create a JSON string like this : {"1":["id","title","date","txt"],"2":[......and so on...

这样做是否正确?

如何解析此字符串以便我可以获取每个新闻项目对象jQuery以便我可以访问attr。

How can I parse this string so that I can get each news item object in jQuery so that I can access attr.

像这样:

obj.id,
obj.title

或者如果这是创建JSON字符串的错误方法请用jQuery中的解析示例提出一些更好的方法。

Or if this is wrong way of creating JSON string, please suggest some better way with example of parsing in jQuery.

推荐答案

我相信您正在向后组织数据。您似乎想要使用 NewsItems 的数组,如果是这样,那么您的java JSON生成代码应该如下所示:

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:

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();
}

现在你的JSON字符串看起来像这样:

Now your JSON string will look something like this:

[{"id": "someId", "title": "someTitle", "date": "dateString", "txt": "someTxt"},
 {"id": "someOtherId", "title": "someOtherTitle", "date": "anotherDateString", "txt": "someOtherTxt"},
 ...]

假设你的NewsItem gettors返回字符串。 JSONObject方法 put 也被重载以获取原始类型,因此,例如,你的 getId 返回 int ,然后它将被添加为一个裸JSON int 。我假设 JSONObject.put(String,Object)对值调用 toString ,但我不能验证这一点。

Assuming that your NewsItem gettors return Strings. The JSONObject method put is overloaded to take primitive types also, so if, e.g. your getId returns an int, then it will be added as a bare JSON int. I'll assume that JSONObject.put(String, Object) calls toString on the value, but I can't verify this.

现在在javascript中,您可以直接使用这样的字符串:

Now in javascript, you can use such a string directly:

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

这篇关于使用JSONObject和JSONArray创建json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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