Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:44:35+00:00 2026-06-08T05:44:35+00:00

I have data like this: NewsItem : id title date txt There may be

  • 0

I have data like this:

NewsItem :

  • id
  • title
  • date
  • txt

There may be many NewsItems say 10. I have to send them to 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...

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:44:37+00:00Added an answer on June 8, 2026 at 5:44 am

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

    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"},
     ...]
    

    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.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have data like this: <option value=abc >Test - 123</option> <option value=def >Test
I have a data like this. Is there any way to smoothen my plot
I have data like this (col2 is of type Date) | col1 | col2
If I have data like this DF <- data.frame( date = seq(Sys.Date()-1000, len=1000, by=1
I have data in an MYSQL database that looks like this: Project Date Time
I have data like this: Team | goal a | 5 b | 8
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ
I have MYSQL data like this id | number 1 | 3 4 |
I have some data like this : 1 TC1 PASS 2 TC2 FAIL 3
I have some data like this: -1,2752 -1,4735 1 -0,5500 1,3287 2 -1,6293 -2,1460

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.