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 7179979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:15:07+00:00 2026-05-28T17:15:07+00:00

lets say I have a String like this [{ name : Ronald , firstname

  • 0

lets say I have a String like this

[{ "name" : "Ronald" , "firstname" : "Ruck"} , { "name" : "Yunchin" , "firstname" : "Cha"} , { "name" : "Klaus" , "firstname" : "Mixer"}]

Sometimes the string is shorter/longer, important is that at start there is [{ and at the end }]. I would like to read just the name and firstname, put a name + firstname to one String and then put every single Name+Firstname-String in an array. Ok sounds weird? If I would iterate my new array with these names there should be an output: Ronald Ruck, Yunchin Cha, Klaus Mixer… Any ideas? I know something with regex but im not good at it. Thank you!

Edit:
Yes it looks like a Json. But if Im using a json reader like http://code.google.com/p/json-simple/ I get an error: Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject At first I tought its because there is an “[” at the start and a “]” at the end. Without that I get: Unexpected token COMMA(,) at position 44. So its maybe not a real json? I could try http://jackson.codehaus.org/ too, but im not sure that will work.

Edit2:
Ok everything is fine 🙂 With json-simple its working, just have to use the JSONArray correctly. The solution with the Gson Lib is ok too, thank you all.

  • 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-05-28T17:15:08+00:00Added an answer on May 28, 2026 at 5:15 pm

    The string you provided is a valid json string. You can check the validity here. You can use Gson library to parse json string in java.

    Read this Tutorial first than my code will make more sense to you.

    import com.google.gson.Gson;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonParser;
    
    String json1 = "[{\"Name\":\"Ronald\",\"Firstname\":\"Reagan\"},{\"Name\":\"Chris\",\"Firstname\":\"Jeoff\"}]";
    JsonElement json = new JsonParser().parse(json1);
    JsonArray array= json.getAsJsonArray();    
    Iterator iterator = array.iterator();    
    while(iterator.hasNext()){
    
    JsonElement json2 = (JsonElement)iterator.next();
         
    Gson gson = new Gson();
    GetResult gresult = gson.fromJson(json2, GetResult.class);
    System.out.println("Name:" + gresult.getName());
    System.out.println("FirstName:" + gresult.getFirstname());
    

    Now, the corresponding Getter and Setter class for the above code:

     public String Name;
     public String Firstname; //Note the variable names are same as the name in Json String.
    
     public String getFirstname() {
     return Firstname;
     }
     public String getName() {
     return Name;
     }
    

    Output =

    Name:Ronald

    FirstName:Reagan

    Name:Chris

    FirstName:Jeoff

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have an array like this: string [] Filelist = ... I
Lets say you have an XML like like this: <data> <messages> <message name=Person id=P>
lets say I have a class like this: public class MyClass { private String
Lets say I have a string COLIN. The numeric value of this string would
Lets say a have a string such as this one: string txt = Lore
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
Lets say I have class Person { public Person(int age, string name) { Age
Lets say I have a set of model classes like this: public class Person
Lets say I have an object class Person { public string Name { get;

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.