-4

I have a string that looks like this

[
    {"last_name":"Aiken","first_name":"George","state":"VT","party":"R"},
    {"last_name":"Allott","first_name":"Gordon","state":"CO","party":"R"},
    {"last_name":"Anderson","first_name":"Clinton","state":"NM","party":"L"},    
    {"last_name":"Bartlett","first_name":"Edward","state":"AK","party":"D"}
]

That is stored in a variable. How would I add this string to an array so that I can then access the data (no punctuation) is needed.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
RandomNumberFun
  • 638
  • 2
  • 8
  • 25
  • You may want to look up this thing called **JSON** – Brian Roach Jul 31 '13 at 03:42
  • This is coming from a json file that has been parsed. I need to create an array with this data. Not sure how. – RandomNumberFun Jul 31 '13 at 03:43
  • I am not sure what you mean? – RandomNumberFun Jul 31 '13 at 03:44
  • In which form/order do you want to store data in array? Means key and values in two different arrays or something else? – Shailesh Saxena Jul 31 '13 at 03:45
  • 3
    Check out google. It's really helpful for questions like this... – jahroy Jul 31 '13 at 03:46
  • Duplicate of [this](http://stackoverflow.com/q/6402115/778118)... – jahroy Jul 31 '13 at 03:47
  • [GSON](http://en.wikipedia.org/wiki/GSON) can be useful for conversion of json to java object – swapy Jul 31 '13 at 03:48
  • 1
    && And then some. There's probably 1000 duplicates at this point. – Brian Roach Jul 31 '13 at 03:48
  • This is for a class. We are using Blue Jay, and we using java's standard libraries, so I am not sure if that would work or not. – user1787331 1 min ago edit – RandomNumberFun Jul 31 '13 at 03:49
  • Well... if you can't use libraries your teacher probably wants you to do it on your own. Is there a policy in the class about begging for help online without putting in any effort on your own? – jahroy Jul 31 '13 at 03:51
  • This is only one part of the program I am working on. I am asking on here because this is place to ask questions? I am not asking you or anyone else to write this program for me. Please and Thank you. – RandomNumberFun Jul 31 '13 at 03:55
  • 1
    @user1787331 - "How do I do X" is not a valid question on StackOverflow. Ignoring that, there's literally hundreds of questions and answers already on SO that answer this. Use the search. Use Google. The answer is: Use a JSON parser, or write your own - details for the format can be found at http://json.org . I *highly* suggest the former. – Brian Roach Jul 31 '13 at 03:56
  • @BrianRoach Most answers to this question use parsers that I cannot use in bluejay. This is why I asked. I have looked for a solution but was unable to find one. If you would be kind enough to point me to the 1 of the "hundreds" of solutions to what I am asking that would be great. – RandomNumberFun Jul 31 '13 at 03:59
  • 1
    @user1787331 - If your answer to these comments is "I can't use an industry standard JSON parser" ... then your questions is "How do I write a JSON parser?" which again, would not be a valid question for StackOverflow. Please understand there's no malice involved here, this is simply far beyond the defined scope of this site. – Brian Roach Jul 31 '13 at 04:02
  • possible duplicate of [Parse JSON string into an array](http://stackoverflow.com/questions/11461142/parse-json-string-into-an-array) – Ashish Aggarwal Jul 31 '13 at 04:02
  • A quick look at the [_BlueJ website_](http://www.bluej.org/help/faq.html#use-library) suggests there is no reason you can't use an external library. There's no way your professor expects you to write your own JSON parser if you're a first year student. – jahroy Jul 31 '13 at 04:59

2 Answers2

1

The string you have mentioned is a JSON string.

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

You should use a parser to parse the JSON string and fetch its elements. There are multiple JSON parser available on internet such as:

  1. Jackson
  2. JSON Simple
  3. GSON
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • This is for a class. We are using Blue Jay, and we using java's standard libraries, so I am not sure if that would work or not. – RandomNumberFun Jul 31 '13 at 03:47
  • [This is the most basic JSON library](http://json.org/java/) for parsing JSON. It's open source, so if you're not allowed to use libraries, you can look at the source code and do something similar (or identical) on your own. – jahroy Jul 31 '13 at 03:50
  • 2
    @user1787331 Writing a JSON parser will be an entire project in itself and there is no point re-inventing the wheel. If existing parsers solve your purpose then try to leverage the use of them. – Juned Ahsan Jul 31 '13 at 03:52
0

JSON is built on two structures:

  1. A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  2. An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence. ---> source & more info: http://www.json.org/

You just need to parse json object and iterate through json array to store the values in array. These links may be helpful to you:

Link 1 to parse json

Link 2 to parse json

Link 3 to parse json

Community
  • 1
  • 1
Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28